【发布时间】:2018-05-18 14:31:48
【问题描述】:
我在 mongodb 中有以下文档
{
"_id" : ObjectId("5a25f1b19c72e07ffc50a37d"),
"name" : "test",
"category" : ObjectId("5a1e6622a7739fb64c8c530b"),
"recentDeveloperComment" : ObjectId("5a25f1b19c72e07ffc50a37c"),
"fileName" : "test2.xlsx",
"rawData" : ObjectId("5a21079ef99e75b2140fb77a"),
"group" : {
"year" : 2017,
"month" : 1,
"category" : "test1"
},
"unlocked" : false,
"status" : "Pending",
"updatedAt" : ISODate("2017-12-05T01:08:40.465Z"),
"createdAt" : ISODate("2017-12-05T01:08:40.465Z"),
"__v" : 0
}
我可以通过使用以下带有 mongoose 的 find 查询来匹配它
Repo.find({'group.year' : year, 'group.month' : month, 'group.category' : category, name:repo})
.exec((err, repoResult) => {
//matches one document
})
但是,使用以下汇总 $match 我无法得到相同的结果
Repo.aggregate([
{
$match:
{
$and:[
{'group.year' : year},
{'group.month' : month},
{'group.category' : category},
{name : repo}
]
}
},
]).exec((err, commitsResult) => {
//matches zero document
});
这是为什么?
【问题讨论】: