我的结构如下:
{
day: x,
events:
[
{
year: y,
info: z
}
]
}
到目前为止,我创建了以下查询,它没有返回错误,但也显示了任何内容(这是错误的)。
db.days.aggregate([
{
$match:
{
$and:
[
{
'day': 'March_13'
},
{
'events.year': '1870'
},
{
'events.info': {$regex: "./French./"}
}
]
}
},
{
$unwind: {path: "$events"},
},
{
$match:
{
'info': { $regex: '.*French.*'}
}
}])
根据我所读到的内容,我需要按_id进行分组,但我不知道如何用满足第二个$match的对象重新创建数组。
你能看一看,也许告诉我为什么最初的查询不起作用,并建议我进行组块吗?
这里有一些样本数据:
{
"day" : "March_13",
"events" :
[
{
"year" : "1929",
"info" : "Peter Breck, American actor (d. 2012)"
},
{
"year" : "1929",
"info" : "Joseph Mascolo, American actor"
},
{
"year" : "1929",
"info" : "Zbigniew Messner, Polish economist and politician, 9th Prime Minister of the Republic of Poland (d. 2014)"
},
{
"year" : "1929",
"info" : "Bunny Yeager, American model and photographer (d. 2014)"
}
]
}
如果我能成功地用"美国"这个词进行查询:
{
"day" : "March_13",
"events" :
[
{
"year" : "1929",
"info" : "Peter Breck, American actor (d. 2012)"
},
{
"year" : "1929",
"info" : "Joseph Mascolo, American actor"
},
{
"year" : "1929",
"info" : "Bunny Yeager, American model and photographer (d. 2014)"
}
]
}
基本上,我想检查字段信息是否包含搜索到的单词,如果包含,我会将其保留在数组中。
对于上面的示例,您需要尝试运行以下聚合管道以获得所需的结果:
db.days.aggregate([
{
"$match": {
"day" : "March_13",
"events.year": "1929",
"events.info": /American/
}
},
{ "$unwind": "$events" },
{
"$match": {
"day" : "March_13",
"events.year": "1929",
"events.info": /American/
}
},
{
"$group": {
"_id": "$_id",
"day": { "$first": "$day" },
"events": { "$push": "$events" }
}
}
])
样本输出
/* 0 */
{
"result" : [
{
"_id" : ObjectId("5706b38dcc578484faab815f"),
"day" : "March_13",
"events" : [
{
"year" : "1929",
"info" : "Peter Breck, American actor (d. 2012)"
},
{
"year" : "1929",
"info" : "Joseph Mascolo, American actor"
},
{
"year" : "1929",
"info" : "Bunny Yeager, American model and photographer (d. 2014)"
}
]
}
],
"ok" : 1
}
如果我们可以将$regex
与$cond
运算符或$filter
运算符一起使用,这将非常简单。也就是说,您有两种选择,第一种是使用聚合框架(如本答案中所述)和本地聚合管道运算符,它们在C++中编码时会更快,但在管道中您需要使用$unwind
运算符,如果您处理的是大数组,取消规范后文档的大小可能超过16MB,在这种情况下聚合查询将失败。如果发生这种情况,可以使用mapReduce
function map() {
var events = this.events.filter(function(element) {
return (/American/i).test(element.info) && element.year === "1929";
});
emit(this.day, events);
}
db.collection.mapReduce(
map,
function(key, value) {},
{ out: { inline: 1 } },
{ query: { "day": "March_13" } }
)
哪个返回:
{
"results" : [
{
"_id" : "March_13",
"value" : [
{
"year" : "1929",
"info" : "Peter Breck, American actor (d. 2012)"
},
{
"year" : "1929",
"info" : "Joseph Mascolo, American actor"
},
{
"year" : "1929",
"info" : "Bunny Yeager, American model and photographer (d. 2014)"
}
]
}
],
"timeMillis" : 27,
"counts" : {
"input" : 1,
"emit" : 1,
"reduce" : 0,
"output" : 1
},
"ok" : 1
}