在一个集合中,我有多个这样的数据集:
{
"_id" : "YAEYvJ7tvXxTvnFtv",
"article" : [
{
"title" : "Article 1",
"detail" : [
{
"edition" : "second edition",
"year" : 2000,
}
]
}
]
}
{
"_id" : "BpsCfbhZuoXfEvREG",
"article" : [
{
"title" : "Article 2",
"detail" : [
{
"edition" : "third edition",
"year" : 2010,
}
]
}
]
}
{
"_id" : "xJD8gRTHMfsBb9TTG",
"article" : [
{
"title" : "Article 3",
"detail" : [
{
"edition" : "first edition",
"year" : 2000,
}
]
}
]
}
是否可以过滤年份= 2000的数据集?所以在这个例子中,我想获得" YAEYvJ7tvXxTvnFtv"的数据集。和" xJD8gRTHMfsBb9TTG"。
collection.find({year: 2000});
答案 0 :(得分:2)
尝试:
db.collection.find({"article.detail.year": 2000});
如果你想更精确,你可以spicify阵列位置:
db.collection.find({"article[0].detail[0].year": 2000});
答案 1 :(得分:1)
您正在尝试查询嵌套或嵌入的文档(http://docs.mongodb.org/manual/reference/method/db.collection.find/#query-embedded-documents)
您的查询应该是
collection.find({article : {detail : {year : 2000}}});