如何获取对象数据以及仅匹配的子数组数据

时间:2018-10-01 13:33:09

标签: javascript node.js mongodb

我写了一个api来匹配mongodb中的subarry数据,但是我只想获取匹配的subarray数据和对象的少量数据。

这是我的查询:

db.collection.aggregate([
  {
    "$project": {
      "projectId" : 1,
      "typesOfServices": {
        "$map": {
          "input": "$typesOfServices",
          "as": "store",
          "in": {
            "sourceLanguage": "$$store.sourceLanguage",
            "targetLanguage": "$$store.targetLanguage",
            "serviceType": "$$store.serviceType",
            "rate": "$$store.rate",
            "uom": "$$store.uom",
            "typeOfDocument": "$$store.typeOfDocument",
            "tools": "$$store.tools",
            "font": "$$store.font",
            "noOfResources": "$$store.noOfResources",
            "assignedOn": "$$store.assignedOn",
            "_id": "$$store._id",
            "assignedTo": {
              "$filter": {
                "input": "$$store.assignedTo",
                "as": "assignedTo",
                "cond": {
                  "$setIsSubset": [
                    [
                      req.user.user.empId
                    ], [
                      "$$assignedTo"
                    ]
                  ]
                }
              }
            }
          }
        }
      }
    }
  }
])

我的输出是:

[
    {
        "_id": "5bb1ecf2d17155223c86411b",
        "projectId": "NA",
        "typesOfServices": [
            {
                "sourceLanguage": "BahasaIndonesia",
                "targetLanguage": "Avestan",
                "serviceType": "Subtitling",
                "rate": 56,
                "uom": "Days",
                "typeOfDocument": "UIStrings,Corporate communication documents",
                "tools": "Office 2016,Adobe RoboHelp",
                "font": "fgdjf",
                "assignedOn":"Mon Oct 01 2018 15: 47: 53 GMT+0530 (India Standard Time)","_id":"5bb1ecf2d17155223c86411d","assignedTo":["qqq1234"]
            }, {
                    "sourceLanguage": "Bahasa Indonesia",
                    "targetLanguage": "Avestan",
                    "serviceType": "Subtitling",
                    "rate": 56,
                    "uom": "Days",
                    "typeOfDocument": "UI Strings,Corporate communication documents",
                    "tools": "Office 2016,Adobe RoboHelp",
                    "font": "fgdjf",
                    "_id": "5bb1ecf2d17155223c86411c",
                    "assignedTo": []
            }
        ]
    }, {
        "_id": "5bb1f36b691ae2195819b510",
        "projectId": "NA",
        "typesOfServices": [
            {
                "sourceLanguage": "Bangladeshi Bengali",
                "targetLanguage": "Avestan",
                "serviceType": "Subtitling",
                "rate": 45,
                "uom": "English Word",
                "typeOfDocument": "UI Strings",
                "tools": "Office 2016",
                "font": "fg",
                "assignedOn": "Mon Oct 01 2018 15:48:29 GMT+0530 (India Standard Time)",
                "_id": "5bb1f36b691ae2195819b512",
                "assignedTo": [
                    "qqq1234"
                ]
            }, {
                "sourceLanguage": "Bangladeshi Bengali",
                "targetLanguage": "Avestan",
                "serviceType": "Subtitling",
                "rate": 45,
                "uom": "English Word",
                "typeOfDocument": "UI Strings",
                "tools": "Office 2016",
                "font": "fg",
                "_id": "5bb1f36b691ae2195819b511",
                "assignedTo": []
            }
        ]
    }
]

问题在于,它与AssignedTo字段匹配,但也在获取空数据(如果不匹配)。

任何人都可以调查一下,过去两天我一直在头。 ?

0 个答案:

没有答案