MongoDB-如何查找数组中的所有对象? [英] MongoDB - How to find all objects within an array?
问题描述
我正在尝试在名为programme
的数组中查找 all 个元素.运行db.base.find({"programme.title":"News"},{"programme.$.title":"News"}).pretty()
的结果是:
I'm trying to find all elements within an array called programme
. The result of running db.base.find({"programme.title":"News"},{"programme.$.title":"News"}).pretty()
is:
{
"_id" : ObjectId("55a48dd3e72db9d63e53478b"),
"programme" : [
{
"attributes" : {
"start" : "20150713010000 -0400",
"stop" : "20150713013000 -0400",
"channel" : "I47176.labs.zap2it.com"
},
"title" : "News",
"category" : "News",
"episode-num" : "SH01831861.0000",
"new" : true
}
]
}
但是还有很多个条目,称为新闻",而不仅仅是一个,如上所示.
But there are many more entries called "News", not just one, as shown above.
我也尝试过db.base.find({"programme.title":"News"}).pretty()
,但这只会导致列出一堆随机条目,无论它们的标题是否为新闻".
I've also tried db.base.find({"programme.title":"News"}).pretty()
but that just results in listing a bunch of random entries, whether or not they have the title of "News".
非常感谢您的帮助,并强制性地道歉"noob问题".
Thanks a lot for the help, and obligatory "apologies for noob question".
推荐答案
您可以借助聚合来实现以下目标:
You can achieve with the help of Aggregation as below :
db.base.aggregate([
{$unwind : "$programme"},
{$match : { "programme.title" : "News" } },
{$group : { "_id" : "$_id" , "programme" : { $push: "$programme" } } }
]);
这篇关于MongoDB-如何查找数组中的所有对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!