简体   繁体   中英

$unwind multiple arrays present in the embedded documents and show each array as single document as output in mongoDB

Refer below code. In this, field scenario is a embedded document which has has arrays and I want to showcase each array as a single document in the output. Note that each array contains embedded document in it so it would be helpful to get the code which extracts fields from those too. I'm not using java to query. Would be using external BI application which would be integrated in. Think I should also mention that i'm using NoSQLBooster for MongoDB application to create these queries.

{
    "_id": {
        "$oid": ""
    },
    "organisationId": "",
    "bcpId": "",
    "bcpName": "",
    "bcpDescription": "",
    "biaEntity": {},
    "version": "0.01",
    "status": "PENDING",
    "primaryBridgeNumber": "1",
    "alternateBridgeNumber": "2",
    "scenario": [{
        "_id": {
            "$oid": "5e3ab709367d2c5f5826c6fd"
        },
        "scenario": "",
        "strategies": [{
            "mdmStrategy": {},
            "strategy": {
                "pSIStrategyDetails": {
                    "scenarioName": "",
                    "strategyName": "",
                    "rto": "",
                    "sustainablePeriod": {

                    },
                    "description": "1",
                    "primaryContact": {
                    },
                    "secondaryContact": {
                    },
                    "recoverySite": {

                    }
                },
                "pSICriticalStaff": {},
                "specialRequirement": [{

                }, {

                }, {

                }, {

                }, {

                }]
            },
            "createdOn": {},
            "updatedOn": {}
        }, {
            "mdmStrategy": {},
            "strategy": {
                "pSIStrategyDetails": {},
                "pSICriticalStaff": {},
                "specialRequirement": [{
                },
                 {
                },
                 {
                },
                 {                  
                }, 
                {

                }]
            },
            "createdOn": {},
            "updatedOn": }
        }],
        "description": "",
        "status": "Active",
        "createdOn": {},
        "updatedOn": {}
    }],
    "updatedOn": {},
    "createdOn": {},
    "business_owner_id": {},
    "bc_coordinator_id": {},
    "backup_business_owner_id": {},
    "backup_business_coordinator_id": {},
    "sme_id": {},
    "_class": "com.bcm.bcp.api.model.BcmBcpEntity"
}

expected output:

{{
"bcpId": "",
"bcpName": "",
"bcpDescription": "",
"version": "0.01",
"status": "PENDING",
"scenario.scenario":"---",
"scenario.strategies.strategy.strategyName":"---",
"scenario.strategies.strategy.rto":"---",
etc...
}{
"bcpId": "",
"bcpName": "",
"bcpDescription": "",
"version": "0.01",
"status": "PENDING",
"scenario.scenario":"---",
"scenario.strategies.strategy.strategyName":"---",
"scenario.strategies.strategy.rto":"---",
etc...
}{
"bcpId": "",
"bcpName": "",
"bcpDescription": "",
"version": "0.01",
"status": "PENDING",
"scenario.scenario":"---",
"scenario.strategies.strategy.strategyName":"---",
"scenario.strategies.strategy.rto":"---",
etc...
}}

"scenario.scenario":"---","scenario.strategies.strategy.strategyName":"---", "scenario.strategies.strategy.rto":"---",

will be coming from the arrays so the output will be number of elements present in the array

U hope this is what you want:

db.collection.aggregate([
  {
    $unwind: "$scenario"
  },
  {
    $unwind: "$scenario.strategies"
  },
  {
    $project: {
      bcpId: 1,
      bcpName: 1,
      bcpDescription: 1,
      version: 1,
      status: 1,
      scenario: {
        scenario: 1,
        strategies: {
          strategy: {
            pSIStrategyDetails: {
              rto: 1,
              strategyName: 1
            }
          }
        }
      }
    }
  }
])

Output:

[
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "bcpDescription": "",
    "bcpId": "",
    "bcpName": "",
    "scenario": {
      "scenario": "",
      "strategies": {
        "strategy": {
          "pSIStrategyDetails": {
            "rto": "",
            "strategyName": ""
          }
        }
      }
    },
    "status": "PENDING",
    "version": "0.01"
  },
  {
    "_id": ObjectId("5a934e000102030405000000"),
    "bcpDescription": "",
    "bcpId": "",
    "bcpName": "",
    "scenario": {
      "scenario": "",
      "strategies": {
        "strategy": {
          "pSIStrategyDetails": {}
        }
      }
    },
    "status": "PENDING",
    "version": "0.01"
  }
]

Explanation: You need to use 2 $unwind operators, as it is like arrays of arrays and a $project operator to display only those fields, which you need.

MongoPlayGroundLink

PS - The question is still unclear.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM