node.js - 从mongodb中的嵌套数组获取数据

标签 node.js mongodb mongoose

我对 mongodb 很陌生。我刚刚使用 mongodb 启动我的项目。在我的项目中,我需要从嵌套数组中获取数据。我知道与此相关的问题太多,但我的问题没有解决。这是 json。

{
    "_id": ObjectId("58805a04469d401ab45943f6"),
    "siteId": 2,
    "guardGender": "MALE",
    "checkoutPoint": [{
        "_id": ObjectId("588098a025bad12cf01936d6"),
        "title": "string",
        "nfcCheckoutPoint": [{
            "_id": ObjectId("5880c14683d042207896f7a4"),
            "title": "yup",
            "intervalTime": "string",
        }]
    }]

} 

我只需要 nfcCheckoutPoint 数组。

"nfcCheckoutPoint": [{
                "_id": ObjectId("5880c14683d042207896f7a4"),
                "title": "yup",
                "intervalTime": "string",
            }]

这是我正在使用的代码

var criteria = {
                   _id: payloadData.siteId,
                   "checkoutPoint._id": payloadData.routeId,
                 "checkoutPoint.nfcCheckoutPoint._id": payloadData.checkpointId
                };
var projection = { nfcCheckoutPoint: { $elemMatch: { _id: payloadData.checkpointId }}}
 var option = {
                 lean: true
              };
Service.SiteService.getSite(criteria, projection, option, function (err, data) {

    if (err) {
        cb(err)
    } else {
        console.log(data)// this give me wrong values
    } 
})      


var getSite = function (criteria, projection, options, callback) {
    options.lean = true;
    Models.Site.find(criteria, projection, options, callback);
};

通过这段代码我得到:

 { "_id": ObjectId("58805a04469d401ab45943f6")}

最佳答案

您可以尝试使用聚合查询

db.getCollection('products').aggregate([
  { "$match": { _id: payloadData.siteId} }, //siteId = ObjectId("58805a04469d401ab45943f6")
  { "$project": {
    "checkoutPoint": {
      "$filter": {
        "input": {
          "$map": {
            "input": "$checkoutPoint",
            "as": "cPoint",
            "in": {
              "_id": "$$cPoint._id",
              "nfcCheckoutPoint": {
                "$filter": {
                  "input": "$$cPoint.nfcCheckoutPoint",
                  "as": "nfcPoint",
                  "cond": {
                    "$eq":  [ '$$nfcPoint._id', payloadData.checkpointId ] //checkpointId = ObjectId("5880c14683d042207896f7a4")
                  }
                }
              }
            }
          }
        },
        "as": "checkoutPoint",
        "cond": { "$eq": [ "$$checkoutPoint._id", payloadData.routeId]}// routeId =ObjectId("588098a025bad12cf01936d6")
      }
    }
  }},
  {$project:{nfcCheckoutPoint:{$arrayElemAt: [ "$checkoutPoint.nfcCheckoutPoint", 0 ]}, _id:0}}
])

然后输出将类似于:

{
    "nfcCheckoutPoint" : [ 
       {
          "_id" : ObjectId("5880c14683d042207896f7a4"),
          "title" : "yup",
          "intervalTime" : "string"
       }
     ]
}

关于node.js - 从mongodb中的嵌套数组获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41757900/

相关文章:

javascript - 从 Graphql 端点返回数据时出错

node.js - LocalStorage 作为请求

javascript - 是否可以从 Node.JS/Javascript 调用 C# 函数

javascript - 查询时 - 是什么意思

node.js - 如何在 Web 应用程序中集成外部依赖项

php - 在 MongoDB 和 php 中更新许多文档

mongodb - deno 连接到 mongodb 失败

Mongodb $match 和 $project 聚合

javascript - Mongoose:更新文档时转换为日期失败

javascript - MongoDb 当存储为字符串时按日期查找