2

I have the below schema

{
 _id: ObjectId,
 account: [{
   accountId: String,
   commonId: string
  }
 ],
 sessions: [{
   sessionId: String,
   commonId: String
  }
 ]
}

I want to combine both these array into one array of objects in aggregate query, where the result should be

{
 _id: ObjectId,
 merged: [{
   accountId: String,
   stringId: String,
   commonId: string
  }
 ],
}

Sample document:

{
  _id: 0,
  account: [
  { 
    accountId: '1234',
    commonId: '0'
  },
  {
    accountId: '1235',
    commonId: '1'
  },
  {
    accountId: '1236',
    commonId: '2'
  }
 ],
 sessions: [
  {
   sessionId: '6781',
   commonId:'0'
  },
  {
   sessionId: '6782',
   commonId:'1'
  },
  {
   sessionId: '6783',
   commonId:'2'
  } 
 ]

expected output:

{
  _id: 0,
  merged: [
  { 
    accountId: '1234',
    sessionId: '6781',
    commonId: '0'
  },
  {
    accountId: '1235',
    sessionId: '6782',
    commonId: '1'
  },
  {
    accountId: '1236',
    sessionId: '6783',
    commonId: '2'
  }
 ],

this or unwinded version of this

How can i do this? Any help is appreciated

4
  • Could you show some sample documents Commented Apr 29, 2020 at 9:17
  • @Ashh added a sample document Commented Apr 29, 2020 at 9:20
  • account and sessions will always have the same size / order? Or we need to check commonId to merge items? Commented Apr 29, 2020 at 9:25
  • @Valijon it can be different, So we need to check commonId to merge Commented Apr 29, 2020 at 10:35

2 Answers 2

1

You can use below aggregation

db.collection.aggregate([
  { "$project": {
    "merged": {
      "$map": {
        "input": "$account",
        "in": {
          "accountId": "$$this.accountId",
          "commonId": "$$this.commonId",
          "sessionId": {
            "$arrayElemAt": [
              "$sessions.sessionId",
              {
                "$indexOfArray": ["$sessions.commonId", "$$this.commonId"]
              }
            ]
          }
        }
      }
    }
  }}
])

MongoPlayground

Sign up to request clarification or add additional context in comments.

Comments

1

Try the query below with $mergeObjects operator:

db.collection.aggregate([
  {
    $project: {
      merged: {
        $map: {
          input: "$account",
          as: "acc",
          in: {
            $mergeObjects: [
              "$$acc",
              {
                $arrayElemAt: [
                  {
                    $filter: {
                      input: "$sessions",
                      as: "ses",
                      cond: {
                        $eq: [
                          "$$ses.commonId",
                          "$$acc.commonId"
                        ]
                      }
                    }
                  },
                  0
                ]
              }
            ]
          }
        }
      }
    }
  }
])

MongoPlayground

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.