【问题标题】:having trouble filtering, boolean issue过滤时遇到问题,布尔问题
【发布时间】:2021-08-18 07:52:04
【问题描述】:

“open”假设采用布尔值,我应该能够用它过滤结果,但结果是一个空数组,请原谅凌乱的代码

让 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}})
  }
  console.log(queryToMatch)
  res.json("works")

  Project.aggregate(queryToMatch, (err, data) => {
    //console.log(data)
    res.json(data)
  })

【问题讨论】:

    标签: json database mongodb boolean filtering


    【解决方案1】:

    您没有包含架构或您尝试查询的示例文档。出于这个原因,我的回答可能与您的预期不同。

    如果我添加以下文件...

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-22
      • 2021-04-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-01
      • 2021-10-21
      • 2013-03-19
      相关资源
      最近更新 更多