Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query mongodb with DBRef

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?

like image 628
Gelin Luo Avatar asked Jun 01 '11 01:06

Gelin Luo


People also ask

What is Dbref in MongoDB?

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.

What is Dbref in MongoDB spring boot?

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.

How do I use $in in MongoDB?

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.

How do I reference in MongoDB?

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.


1 Answers

Got it:

db.post.find({'author.$id': 'foo'}) 
like image 112
Gelin Luo Avatar answered Sep 23 '22 05:09

Gelin Luo