How to query mongodb with DBRef

ReferenceMongodbDbref

Reference Problem Overview


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?

Reference Solutions


Solution 1 - Reference

Got it:

db.post.find({'author.$id': 'foo'})

Solution 2 - Reference

This db.post.find('author.$id': 'foo') has missing the {}, so the correct sentence is:

db.post.find({'author.$id': 'foo'})

Also this can be achieved with:

db.post.find({'author': DBRef("user", ObjectId('foo'))})

But is more compact and practical the first way.

Solution 3 - Reference

You can use the .$id reference but it will ignore any indexes on those fields. I would suggest ignoring that method unless you are querying it directly via the terminal or want to look up something quickly. In using large collections you will want to index the field and query it using the below method.

If you want to use an index query using the following:

db.post.find('author' : { "$ref" : 'user', "$id" : 'foo' , "$db" :'database_name' })

If foo is an object id

db.post.find('author' : { "$ref" : 'user', "$id" : ObjectId('foo') , "$db" :'database_name' })

You can create an index on author by

db.post.ensureIndex( {'author' : 1 } );

Solution 4 - Reference

For anyone looking for a Java solution to this then if you are using mongojack its really easy:

collection.find(DBQuery.is("user", new DBRef(user.getId(), User.class)));

Where collection is a JacksonDBCollection.

Solution 5 - Reference

In mongoengine you should just use the instance of the referenced object. It should have the ID set. Suppose the author is the Author document instance. So using this:

Post.objects(author__eq=author)

you can go through all posts of this author. Post.author should be defined as ReferenceField

Solution 6 - Reference

Using Mongo 2.4.1 version

This is how you do it on command line for OLA collection where @DBRef dbrefName

db.OLA.find({"dbrefName.someFieldValue" : "Personal"});

Exact query

db.OLA.find({"dbrefName.$id" : ObjectId("1234")});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGelin LuoView Question on Stackoverflow
Solution 1 - ReferenceGelin LuoView Answer on Stackoverflow
Solution 2 - ReferenceMariano RuizView Answer on Stackoverflow
Solution 3 - ReferenceRedPhoenixView Answer on Stackoverflow
Solution 4 - ReferencedannrobView Answer on Stackoverflow
Solution 5 - ReferenceKostanosView Answer on Stackoverflow
Solution 6 - Referencejava_dudeView Answer on Stackoverflow