How to query mongodb with DBRef

Gelin Luo picture Gelin Luo · Jun 1, 2011 · Viewed 82.6k times · Source

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?

Answer

Gelin Luo picture Gelin Luo · Jun 1, 2011

Got it:

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