Mongodb 选择字段以返回数组中的嵌入文档
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9201743/
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
Mongodb select field to return embedded document in array
提问by Fredrik L
Given the example doc below:
鉴于下面的示例文档:
{
"_id" : "2",
"objects" : [{
"_id" : "1",
"name" : "embedded "
},{
"_id" : "2",
"name" : "embedded "
},{
"_id" : "3",
"name" : "embedded "
}],
"name" : "gloss2"
}
Is it possible to return only one subdocument? That way I don't have to select the whole parent object, get the list, and loop through the list to get the object in question.
是否可以只返回一个子文档?这样我就不必选择整个父对象,获取列表,然后遍历列表来获取有问题的对象。
{
"_id" : "2",
"name" : "embedded"
}
回答by Gates VP
Is it possible to return only one subdocument?
是否可以只返回一个子文档?
Yes, but not the way you want. If you do the following, you will only get back the first element of the array:
是的,但不是你想要的方式。如果您执行以下操作,您将只会返回数组的第一个元素:
coll.find({_id:'2'}, { 'objects.0': 1})
However, what you really want is something that looks like the following:
但是,您真正想要的是如下所示的内容:
coll.find({_id:'2', 'objects._id': '3'}, { 'objects.$' : 1})
Of course, that does not actually work in MongoDB.
当然,这实际上在 MongoDB 中不起作用。
Looking at your other question, this is one of the reasons to use the "embedded object" instead of the "array of objects". With "embedded object" you could do the following:
看看你的另一个问题,这是使用“嵌入式对象”而不是“对象数组”的原因之一。使用“嵌入对象”,您可以执行以下操作:
coll.find({_id:'2'}, {'objects.3': 1}) // where 3 is the id of the third object
This lets you pick just the "embedded objects" you need.
这使您可以只选择所需的“嵌入对象”。
That way I don't have to select the whole parent object...
这样我就不必选择整个父对象......
The thing with MongoDB is that the parent document is alwaysfetched. Queries return top-level documents. This is baked into the whole architecture. Even if you request just a slice of the document the server still has to load the entire document into memory before serving you the requested piece.
MongoDB 的问题是始终获取父文档。查询返回顶级文档。这融入了整个架构。即使您只请求文档的一部分,服务器仍然必须在为您提供请求的部分之前将整个文档加载到内存中。
The only way around this may be the new Aggregation Framework, but that is not yet in the stable branch.
解决这个问题的唯一方法可能是新的Aggregation Framework,但这还没有在稳定分支中。
回答by kop
You can do it with mongo > 2.2 version.
您可以使用 mongo > 2.2 版本来完成。
db.collection.find({'objects._id':1},{'objects.$': true})
db.collection.find({'objects._id':1},{'objects.$': true})
But you just get the first match element http://docs.mongodb.org/manual/reference/projection/positional/
但是你只得到第一个匹配元素 http://docs.mongodb.org/manual/reference/projection/positional/
回答by Eve Freeman
You can return one subdocument, but you can't return one element from an array. Sorry.
您可以返回一个子文档,但不能从数组中返回一个元素。对不起。
回答by Victor
In the latest version of mongodb (now is 2.6) there is a projection, $elemMatch
, that help us in this case.
If it helps someone, there are some examples in this page: http://docs.mongodb.org/manual/reference/operator/projection/elemMatch/#proj._S_elemMatch
在最新版本的 mongodb(现在是 2.6)中有一个投影,$elemMatch
,在这种情况下可以帮助我们。如果它对某人有帮助,则此页面中有一些示例:http: //docs.mongodb.org/manual/reference/operator/projection/elemMatch/#proj._S_elemMatch
回答by Naresh Kumar
Mongodb 3.2 introduces $elemMatch with that you can get only one first matched document from the array of documents.
Mongodb 3.2 引入了 $elemMatch ,您只能从文档数组中获取第一个匹配的文档。
db.sample.find({_id:"2"},{objects:{$elemMatch:{_id:"2"}}})