您没有包含架构或您尝试查询的示例文档。出于这个原因,我的回答可能与您的预期不同。
如果我添加以下文件...
db.test.insert(
{
"_id" : ObjectId("60b44ae6af90ae8f4deca589"),
"name": "project",
"open": "open",
"issue_title": "mytitle",
"issue_text": "mytext",
"created_by": "barry",
"assigned_to": "john",
"status_text": "mystatus",
"issues": [
{
"issue_type": "type1",
"issue_date": new Date
},
{
"issue_type": "type2",
"issue_date": new Date
},
{
"issue_type": "type3",
"issue_date": new Date
},
]
}
)
我可以运行以下 mongo shell 命令...
var _id = ObjectId("60b44ae6af90ae8f4deca589");
var open = "open";
var issue_title = "mytitle";
var issue_text = "mytext";
var created_by = "barry";
var assigned_to = "john";
var status_text = "mystatus";
let queryToMatch = [{"$match": {"name": "project"}},{$unwind:"$issues"}];
if(_id != undefined){
queryToMatch.push({$match: {"_id": _id}})
}
if(open != undefined){
queryToMatch.push({$match: {"open": open}})
}
if(issue_title != undefined){
queryToMatch.push({$match:{"issue_title": issue_title}})
}
if(issue_text != undefined){
queryToMatch.push({$match:{"issue_text": issue_text}})
}
if(created_by != undefined){
queryToMatch.push({$match:{"created_by": created_by}})
}
if(assigned_to != undefined){
queryToMatch.push({$match:{"assigned_to": assigned_to}})
}
if(status_text != undefined){
queryToMatch.push({$match:{"status_text": status_text}})
}
...那么我可以发出以下汇总语句...
db.test.aggregate(queryToMatch).pretty()
...我得到以下结果...
{
"_id" : ObjectId("60b44ae6af90ae8f4deca589"),
"name" : "project",
"open" : "open",
"issue_title" : "mytitle",
"issue_text" : "mytext",
"created_by" : "barry",
"assigned_to" : "john",
"status_text" : "mystatus",
"issues" : {
"issue_type" : "type1",
"issue_date" : ISODate("2021-05-31T02:46:04.670Z")
}
}
{
"_id" : ObjectId("60b44ae6af90ae8f4deca589"),
"name" : "project",
"open" : "open",
"issue_title" : "mytitle",
"issue_text" : "mytext",
"created_by" : "barry",
"assigned_to" : "john",
"status_text" : "mystatus",
"issues" : {
"issue_type" : "type2",
"issue_date" : ISODate("2021-05-31T02:46:04.670Z")
}
}
{
"_id" : ObjectId("60b44ae6af90ae8f4deca589"),
"name" : "project",
"open" : "open",
"issue_title" : "mytitle",
"issue_text" : "mytext",
"created_by" : "barry",
"assigned_to" : "john",
"status_text" : "mystatus",
"issues" : {
"issue_type" : "type3",
"issue_date" : ISODate("2021-05-31T02:46:04.670Z")
}
}
看来您的代码确实可以在 mongo shell 的上下文中工作。我的建议是先将 $match 阶段组合在一起,然后执行 $unwind。我假设您通过push 附加的每个谓词都旨在成为“与”条件,而不是“或”条件。这是经过所有逻辑恶作剧之后聚合的最终结果......
[
{
"$match" : {
"name" : "project"
}
},
{
"$unwind" : "$issues"
},
{
"$match" : {
"_id" : ObjectId("60b44ae6af90ae8f4deca589")
}
},
{
"$match" : {
"open" : "open"
}
},
{
"$match" : {
"issue_title" : "mytitle"
}
},
{
"$match" : {
"issue_text" : "mytext"
}
},
{
"$match" : {
"created_by" : "barry"
}
},
{
"$match" : {
"assigned_to" : "john"
}
},
{
"$match" : {
"status_text" : "mystatus"
}
}
]
也许这是一个更简洁、更易于阅读的查询...
[
{
"$match" : {
"_id" : ObjectId("60b44ae6af90ae8f4deca589"),
"name" : "project",
"open" : "open",
"issue_title" : "mytitle",
"issue_text" : "mytext",
"created_by" : "barry",
"assigned_to" : "john",
"status_text" : "mystatus"
}
},
{
"$unwind" : "$issues"
},
]
请考虑为 $match 谓词创建一个对象,而不是使用 push...
var match = new Object();
match.name = "project";
if(_id != undefined){
match._id = _id;
}
if(open != undefined){
match.open = open;
}
if(issue_title != undefined){
match.issue_title = issue_title;
}
if(issue_text != undefined){
match.issue_text = issue_text;
}
if(created_by != undefined){
match.created_by = created_by;
}
if(assigned_to != undefined){
match.assigned_to = assigned_to;
}
if(status_text != undefined){
match.status_text = status_text;
}
let queryToMatch = [{"$match": match},{$unwind:"$issues"}];
db.test.aggregate(queryToMatch).pretty()