5

I have a question on merging arrays into one array in same field, but they are in different objects. I hope you will understand what I wanted after looking at the code below.

 db.addresses.find().pretty()
{
        "_id" : ObjectId("62a72be40c7db0af0b79d721"),
        "rollingStockNumber" : "922698325786980137200129101715063039706000000",
        "addresses" : [
                "Raximova",
                "Raximova",
                "Raximova",
                "Nazarbek",
                "Oxunboboyeva",
        ],
        "__v" : 0
}
{
        "_id" : ObjectId("62a8727978c18925711d40f2"),
        "rollingStockNumber" : "922698012567076507200129101700057022060000000",
        "addresses" : [
                "Toshkent",
                "Chuqursoy",
                "Chuqursoy",
                "Chuqursoy",
                "Chuqursoy",
                "Chuqursoy",
                "Chuqursoy",
                "Toshkent",
        ],
        "__v" : 0
}
{
        "_id" : ObjectId("62a878d778c18925711d40f7"),
        "rollingStockNumber" : "922720020326980977200102111555058048630000000",
        "addresses" : [
                "Oxangaron",
                "Oxangaron",
                "Oxangaron",
                "Oxangaron",
                "Oxangaron",
                "Oxangaron",
                "Jaloir"
        ],
        "__v" : 0
}

I wanted to merge "addresses" arrays in one array called "allAddresses", used $group, $concat, but failed...

 "allAddresses" : [
                    "Raximova",
                    "Raximova",
                    "Raximova",
                    "Nazarbek",
                    "Oxunboboyeva",
                    "Toshkent",
                    "Chuqursoy",
                    "Chuqursoy",
                    "Chuqursoy",
                    "Chuqursoy",
                    "Chuqursoy",
                    "Chuqursoy",
                    "Toshkent",
                    "Oxangaron",
                    "Oxangaron",
                    "Oxangaron",
                    "Oxangaron",
                    "Oxangaron",
                    "Oxangaron",
                    "Jaloir"
            ]

How can I achieve that? Thanks in advance.

2 Answers 2

4
  1. $group - Group by null and add addresses into addresses array. It returns a nested array.

  2. $project - With $reduce to flatten nested array.

db.collection.aggregate([
  {
    $group: {
      _id: null,
      addresses: {
        $push: "$addresses"
      }
    }
  },
  {
    $project: {
      allAddresses: {
        $reduce: {
          input: "$addresses",
          initialValue: [],
          in: {
            "$concatArrays": [
              "$$value",
              "$$this"
            ]
          }
        }
      }
    }
  }
])

Sample Mongo Playground

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

3 Comments

Thanks, it works. By the way, is it same syntax for Node.js? Can I use like "const allAddresses = Address.aggregate(..........." ?!
Yes, you can use the above pipeline in Node.JS. const response = Address.aggregate(/* Pipeline */); const allAddresses = response[0]["allAddresses];.
Bro, it shows undefined...( How can solve??
1

Easier approach:

  1. Unwind the existing array
  2. Append all the unwind objects

Here is the code:

db.collection.aggregate([
  {
    $unwind: {
       path: "$addresses",
       preserveNullAndEmptyArrays: false,
    },
  },
  {
    $group: {
      _id: null,
      allAddresses: {
        "$push": "$addresses"
      }
    }
  }
])

Comments

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.