如何使用 DBRef 查询 mongodb [英] How to query mongodb with DBRef

查看:26
本文介绍了如何使用 DBRef 查询 mongodb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下数据结构:

suppose I have the following datastructure:

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

如何查询所有引用 user[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);

官方文档和google都找不到答案!

neither can I find the answer from the official document and google!

有人知道吗?

推荐答案

知道了:

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

这篇关于如何使用 DBRef 查询 mongodb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆