1

I have 3 collections:

  1. Plan
  2. Business Process
  3. Recovery Strategy

The three of them store their Primary Key in _id and

I have another collection: planRecoveryStrategy which stores

  1. planId
  2. businessProcessId
  3. recoveryStrategyId

Example documents stored in planRecoveryStrategy:

{   "planId" : "PLN1",
    "processId" : "PCS1",
    "strategyId" : "RES1"
}

{   "planId" : "PLN1",
    "processId" : "PCS2",
    "strategyId" : "RES1"
}

{   "planId" : "PLN1",
    "processId" : "PCS2",
    "strategyId" : "RES2"
}

What is the query to write to get given the planId is 'PCS1'

{   "planId" : "PLN1",
    "processes" : [{
      "processId":"PCS1",
      "processData":{<data from Business Process Dictionary for PCS1>},
      "strategies":[{<data from Recovery Strategy Dictionary for RES1>}]
    },
    {
      "processId":"PCS2",
      "processData":{<data from Business Process Dictionary for PCS1>},
      "strategies":[{<data from Recovery Strategy Dictionary for RES1>}, 
                    {<data from Recovery Strategy Dictionary for RES2>}]
    }]
}

from planRecoveryStrategy?

0

1 Answer 1

1

Try it for grouping data:

db.collection.aggregate([
    {
        $group: {
            _id: {
                planId: "$planId",
                processId: "$processId"
            },
            strategies: { $addToSet: "$strategyId" }
        }
    },
    {
        $group: {
            _id: "$_id.planId",
            planId: { $first: "$_id.planId" },
            processes: {
                $addToSet: {
                    processId: "$_id.processId",
                    strategies: "$strategies"
                }
            }
        }
    }
]);

With sort:

db.collection.aggregate([
    { $sort: { "strategyId": -1 } },
    { $sort: { "processId": 1 } },
    {
        $group: {
            _id: {
                planId: "$planId",
                processId: "$processId"
            },
            strategies: { $addToSet: "$strategyId" }
        }
    },
    {
        $group: {
            _id: "$_id.planId",
            planId: { $first: "$_id.planId" },
            processes: {
                $addToSet: {
                    processId: "$_id.processId",
                    strategies: "$strategies"
                }
            }
        }
    }
]);

With including other collections:

db.collection.aggregate([
    { $sort: { "strategyId": -1 } },
    { $sort: { "processId": 1 } },
    {
        $lookup:
          {
            from: "BusinessProcess",
            localField: "processId", 
            foreignField: "id",
            as: "BusinessProcesses"
          }
    },
    {
        $lookup:
          {
            from: "RecoveryStrategy",
            localField: "strategyId", 
            foreignField: "id",
            as: "RecoveryStrategy"
          }
    },
    {
        $group: {
            _id: {
                planId: "$planId",
                processId: "$processId",
                processData: { $arrayElemAt : ["$BusinessProcesses" , 0] },
            },
            strategies: { $addToSet: "$RecoveryStrategy" }
        }
    },
    {
        $group: {
            _id: "$_id.planId",
            planId: { $first: "$_id.planId" },
            processes: {
                $addToSet: {
                    processId: "$_id.processId",
                    processData: "$_id.processData",
                    strategies: "$strategies"
                }
            }
        }
    }
]);
Sign up to request clarification or add additional context in comments.

3 Comments

How do I select the details of the record from Recovery Strategy collection and store it in strategies array ?
Thank you for the solution. But the requirement changed a bit
@PrateekNarendra I added example with including other collections. Replace foreignField and from attributes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.