Home > Software engineering >  Concatenate N arrays
Concatenate N arrays

Time:02-23

I'm working with mongodb and I'm dealing with aggregate to join two collections, now I would like to convert my result

    "Users" : [ 
        [ 
            "Carl", 
            "John",
            "Ever"
        ], 
        [ 
            "Dani", 
            "GG", 
            "Sussan"
        ],
        .... N arrays

    ]

to this output

    "Users" : [ 
            "Carl", 
            "John",
            "Ever",
            "Dani", 
            "GG", 
            "Sussan",
            ..... M elements from N arrays 
    ]

CodePudding user response:

Query

  • reduce starting from []
  • concat each array member to one flatten array

Test code here

aggregate(
[{"$set":
  {"Users":
   {"$reduce":
    {"input":"$Users",
     "initialValue":[],
     "in":{"$concatArrays":["$$value", "$$this"]}}}}}])
  • Related