suppose I have the following datastructure:
var user = {_id: 'foo', age: 35}; var post = {_id: '...', author: {$ref: user, $id: 'foo'},...};
How can I query all posts which references user[foo]? I tried the following but not work:
db.post.find('author._id': 'foo'); var u = db.user.find({_id: 'foo'}); db.post.find('author': u);
neither can I find the answer from the official document and google!
Anyone has any idea?
DBRefs are references from one document to another using the value of the first document's _id field, collection name, and, optionally, its database name, as well as any other fields. DBRefs allow you to more easily reference documents stored in multiple collections or databases.
DBRefs are similar to manual references in the sense that they also contain the referenced document's _id. However, they contain the referenced document's collection in the $ref field and optionally its database as well in the $db field.
Use the $in Operator to Match Values This query selects all documents in the inventory collection where the value of the quantity field is either 5 or 15. Although you can write this query using the $or operator, use the $in operator rather than the $or operator when performing equality checks on the same field.
MongoDB applications use one of two methods to relate documents: Manual references save the _id field of one document in another document as a reference. Your application runs a second query to return the related data. These references are simple and sufficient for most use cases.
Got it:
db.post.find({'author.$id': 'foo'})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With