Mongodb 在mongoose聚合查询中使用$match

Mongodb 在mongoose聚合查询中使用$match,mongodb,Mongodb,我试图在mongoose聚合查询中使用$match Feed.aggregate([ {$match:{"createdBy":'ObjectId("59d79b6ea918cf1e40ee041f")'}}, { "$lookup": { "from": "comments", "localField": "

我试图在mongoose聚合查询中使用$match

Feed.aggregate([
          {$match:{"createdBy":'ObjectId("59d79b6ea918cf1e40ee041f")'}},
          {
                "$lookup":
                  {
                    "from": "comments",
                    "localField": "_id",
                    "foreignField": "feedId",
                    "as": "feedComment"
                  }
             }
          ]).exec(function(err, results){
              if(err){
                res.json({"success": false,"errorCode": errorCodes.serverError,"error": err});
              }else{
                if(results == null){
                  res.json({"success": false,"errorCode": errorCodes.dataNotFound,"error": "no record found"})
                }else{
                  res.json({"success": true,"data": results});
                }
              }
           })
它给出了黑色的结果数组

但如果没有$match,它就可以工作

提要集合是:

    {
        "_id" : ObjectId("59d878f67e6ba32c60c7410c"),
        "createdBy" : ObjectId("59d79b6ea918cf1e40ee041f"),
        "feedKeywords" : "school,kids",
        "feedDescription" : "Going to school",
        "feedTitle" : "Going to schoo",
        "updatedAt" : ISODate("2017-10-07T06:49:26.100Z"),
        "createdAt" : ISODate("2017-10-07T06:49:26.100Z"),
        "__v" : 0
}
评论集是这样的

    {
        "_id" : ObjectId("59d87a737e6ba32c60c7410e"),
        "createdBy" : ObjectId("59d6562dd3c2be2ba452665e"),
        "feedId" : ObjectId("59d878f67e6ba32c60c7410c"),
        "stars" : "3.5",
        "commentText" : "vewr nice",
        "updatedAt" : ISODate("2017-10-07T06:55:47.305Z"),
        "createdAt" : ISODate("2017-10-07T06:55:47.305Z"),
        "__v" : 0
}

您在$match聚合管道中对createdBy的值使用了单引号,因此该值将被视为字符串,但createdBy的提要集合中的数据是一个对象,因此添加$match时没有检索到数据

请使用此更正的查询

Feed.aggregate([
          {$match:{"createdBy":new mongoose.Types.ObjectId("59d79b6ea918cf1e40ee041f")}},
          {
                "$lookup":
                  {
                    "from": "comments",
                    "localField": "_id",
                    "foreignField": "feedId",
                    "as": "feedComment"
                  }
             }
          ]).exec(function(err, results){
              if(err){
                res.json({"success": false,"errorCode": errorCodes.serverError,"error": err});
              }else{
                if(results == null){
                  res.json({"success": false,"errorCode": errorCodes.dataNotFound,"error": "no record found"})
                }else{
                  res.json({"success": true,"data": results});
                }
              }
           })

同时发布评论集合的示例。(节点:2780)未处理的PromisejectionWarning:未处理的承诺拒绝(拒绝id:1):ReferenceError:ObjectId不正确defined@raju-立即尝试,为ObjectId创建添加了新的运算符,但之前没有。@raju-希望现在可以使用。我已将
mongoose.Types.ObjectId
添加到我们的新操作符中。该错误可能是由于正在使用架构类型object Id
mongoose.Schema.Types.ObjectId
强制转换对象,而我们的查询只需要一个纯ObjectId
mongoose.Types.ObjectId
.Yahoo000,非常感谢,先生。它起作用了。现在我可以在其他查询中使用相同的funda。再次感谢