如何使用 DBRef 查询 mongodb
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6195286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to query mongodb with DBRef
提问by Gelin Luo
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:
如何查询所有引用 user[foo] 的帖子?我尝试了以下但不起作用:
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?
任何人有任何想法?
回答by Gelin Luo
Got it:
知道了:
db.post.find({'author.$id': 'foo'})
回答by Mariano Ruiz
This db.post.find('author.$id': 'foo')
has missing the {}
, so the correct sentence is:
这db.post.find('author.$id': 'foo')
缺少{}
,所以正确的句子是:
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.
但第一种方式更紧凑实用。
回答by RedPhoenix
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.
您可以使用 .$id 引用,但它会忽略这些字段上的任何索引。我建议忽略该方法,除非您直接通过终端查询或想快速查找某些内容。在使用大型集合时,您需要索引该字段并使用以下方法查询它。
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
如果 foo 是一个对象 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 } );
回答by dannrob
For anyone looking for a Java solution to this then if you are using mongoHymanits really easy:
对于任何为此寻找 Java 解决方案的人来说,如果您使用的是mongoHyman,那真的很容易:
collection.find(DBQuery.is("user", new DBRef(user.getId(), User.class)));
Where collection is a HymansonDBCollection.
其中 collection 是 HymansonDBCollection。
回答by Kostanos
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:
在 mongoengine 中,您应该只使用引用对象的实例。它应该设置了 ID。假设作者是作者文档实例。所以使用这个:
Post.objects(author__eq=author)
you can go through all posts of this author. Post.author should be defined as ReferenceField
您可以浏览该作者的所有帖子。Post.author 应定义为 ReferenceField
回答by java_dude
Using Mongo 2.4.1 version
使用 Mongo 2.4.1 版本
This is how you do it on command line for OLA collection where @DBRef dbrefName
这是您在命令行上执行 OLA 集合的方式,其中 @DBRef dbrefName
db.OLA.find({"dbrefName.someFieldValue" : "Personal"});
db.OLA.find({"dbrefName.someFieldValue" : "Personal"});
Exact query
精确查询
db.OLA.find({"dbrefName.$id" : ObjectId("1234")});
db.OLA.find({"dbrefName.$id" : ObjectId("1234")});