How to query mongodb with DBRef

后端 未结 6 1825
时光说笑
时光说笑 2020-11-30 22:10

suppose I have the following datastructure:

var user = {_id: \'foo\', age: 35};
var post = {_id: \'...\', author: {$ref: user, $id: \'foo\'},...};

6条回答
  •  情书的邮戳
    2020-11-30 23:08

    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.

提交回复
热议问题