vapor-community / mongo-provider

MongoDB Provider for Vapor
40 stars 18 forks source link

Referencing other object id #14

Open truemetal opened 7 years ago

truemetal commented 7 years ago

This is more of a question with a potential of becoming a feature request.

This code:

var post = Post(userId: Node(user.id!, content: content)
try post.save()

Produces this:

1

This code:

var post = Post(userId: Node(["$oid" : user.id!]), content: content)
try post.save()

Produces this:

2

But how do I achieve this?

proper
tanner0101 commented 7 years ago

Node doesn't have an identifier type so I don't think it's possible to do through the Fluent API currently.

We'd need to decide on some method for signaling an object id (perhaps oid: before the string?)

As a workaround, you can do (database.driver as? MongoDriver)?.raw(...)

vzsg commented 7 years ago

Most Mongo commands use $something as object keys to separate them from JSON data. I think it would be a clean strategy for the driver to do the same, and @truemetal's example code (from "This code:") should work automatically.

tanner0101 commented 7 years ago

@vzsg that seems like a great solution

ankurp commented 6 years ago

I have a sample application where a ShoppingList model has many Item objects. When I create an Item with the shopping_list_id, then the shopping list model is not able to query to fetch all items that it owns. Here is the repo which has the code https://github.com/ankurp/ShoppingListServer

Does Mongo Provider support parent child relation and is it able to query all children from the database?