Search code examples
mongodbmongodb-querymongodb-aggregation

Concatenating arrays nested within arrays nested within another array


Without going into how exactly I got here, can anyone help me concatenate the contents of these nested arrays using aggregation in mongo:

Concatenating

    {  "missing_users" : [ 
            [ 
                [ 
                    "attachment_5089820813-30177-ulr2vz.csv", 
                    "User not found for 201610-EEC-100-A01-31562"
                ], 
                [ 
                    "attachment_50820160813-30177-ulr2vz.csv", 
                    "User not found for 201610-EEC-001-001-31567"
                ], 
                [ 
                    "attachment_5089820813-30177-ulr2vz.csv", 
                    "User not found for 201610-EEC-001-001-31547)"
                ],        
            ], 
            [ 
                [ 
                    "attachment_508160813-28337-5qpqyb.csv", 
                    "User not found for 201610-ETX-010-001-33550)"
                ], 
                [ 
                    "attachment_50520160813-28337-5qpqyb.csv", 
                    "User not found for 201610-ETX-010-001-33330)"
                ], 
                [ 
                    "attachment_50895813-28337-5qpqyb.csv", 
                    "User not found for 201610-ETX-010-001-33580)"
                ], 
                [ 
                    "attachment_50813-28337-5qpqyb.csv", 
                    "User not found for 201610-ETX-010-001-33450)"
                ], 
                [ 
                    "attachment_508916813-28337-5qpqyb.csv", 
                    "User not found for 201610-ETX-010-001-33550)"
                ]
            ]
       ]
 }

I would be happy with just an array of the strings found within the deeply nested arrays, but I'm also really only interested in the second value of the most deeply nested arrays.

This would be adequate:

results : ["attachment_5089820813-30177-ulr2vz.csv", 
       "User not found for 201610-EEC-100-A01-31562",
       "attachment_5089820813-30177-ulr2vz.csv", 
       "User not found for 201610-EEC-100-A01-31562",
       "attachment_5089820813-30177-ulr2vz.csv", 
       "User not found for 201610-EEC-100-A01-31562"]

But I really just want this:

results : ["User not found for 201610-EEC-100-A01-31762",
           "User not found for 201610-EEC-100-A01-31862",
           "User not found for 201610-EEC-100-A01-35602"]

Solution

  • I was trying this.. does the below help ?

    s1:PRIMARY> db.ary.aggregate([{"$unwind":"$missing_users"},{"$unwind":"$missing_users"},{"$unwind":"$missing_users"},{"$group":{"_id":"$_id","missing_users":{"$push":"$missing_users"}}}]).pretty();
    

    \ output

       {
            "_id" : ObjectId("57aead0a4d7aa623b01b5820"),
            "missing_users" : [
                    "attachment_5089820813-30177-ulr2vz.csv",
                    "User not found for 201610-EEC-100-A01-31562",
                    "attachment_50820160813-30177-ulr2vz.csv",
                    "User not found for 201610-EEC-001-001-31567",
                    "attachment_5089820813-30177-ulr2vz.csv",
                    "User not found for 201610-EEC-001-001-31547)",
                    "attachment_508160813-28337-5qpqyb.csv",
                    "User not found for 201610-ETX-010-001-33550)",
                    "attachment_50520160813-28337-5qpqyb.csv",
                    "User not found for 201610-ETX-010-001-33330)",
                    "attachment_50895813-28337-5qpqyb.csv",
                    "User not found for 201610-ETX-010-001-33580)",
                    "attachment_50813-28337-5qpqyb.csv",
                    "User not found for 201610-ETX-010-001-33450)",
                    "attachment_508916813-28337-5qpqyb.csv",
                    "User not found for 201610-ETX-010-001-33550)"
            ]
    }
    s1:PRIMARY>