3

I'm using MongoDB and this is what my documents look like

{
    "_id": 1,
   "arr1": ["a", "a", "b", "c"],
   "arr2": [1, 4, 2, 3 ],
},
{
    "_id": 2,
   "arr1": ["z", "a", "b", "a"],
   "arr2": [1, 4, 2, 3 ],
}

The arrays in keys arr1 and arr2 always have the same length. I would like to aggregate the data such that can I filter out the values in arr2 based on arr1.

For example, if I filter my collection based on arr1 looking for a, I would like to get something like that:

{ "_id": 1, "arr1": "a", "arr2": 1},
{ "_id": 1, "arr1": "a", "arr2": 4 },
{ "_id": 2, "arr1": "a", "arr2": 3 },
{ "_id": 2, "arr1": "a", "arr2": 3 }

Is there some method to accomplish that using MongoDB?

Thanks,

Uirá

1
  • check answer below. Thanks Commented May 14, 2016 at 16:19

1 Answer 1

2

Try this:

db.col1.aggregate([
   {  $unwind: 
         { path:"$arr2", includeArrayIndex: "i"}},
   {  $project: 
         {
           "arr2":1,
           "arr1": { $slice: ["$arr1","$i",1] }}
          },
   {$unwind: "$arr1"}
   {$match: { "arr1":"a" }}
 ])

Output from first $unwind Stage

{ "_id" : 1, "arr1" : [ "a", "a", "b", "c" ], "arr2" : 1, "i" : NumberLong(0) }
{ "_id" : 1, "arr1" : [ "a", "a", "b", "c" ], "arr2" : 4, "i" : NumberLong(1) }
{ "_id" : 1, "arr1" : [ "a", "a", "b", "c" ], "arr2" : 2, "i" : NumberLong(2) }
{ "_id" : 1, "arr1" : [ "a", "a", "b", "c" ], "arr2" : 3, "i" : NumberLong(3) }
{ "_id" : 2, "arr1" : [ "z", "a", "b", "a" ], "arr2" : 1, "i" : NumberLong(0) }
{ "_id" : 2, "arr1" : [ "z", "a", "b", "a" ], "arr2" : 4, "i" : NumberLong(1) }
{ "_id" : 2, "arr1" : [ "z", "a", "b", "a" ], "arr2" : 2, "i" : NumberLong(2) }
{ "_id" : 2, "arr1" : [ "z", "a", "b", "a" ], "arr2" : 3, "i" : NumberLong(3) }

Out put after second project stage

{ "_id" : 1, "arr1" : [ "a" ], "arr2" : 1 }
{ "_id" : 1, "arr1" : [ "a" ], "arr2" : 4 }
{ "_id" : 1, "arr1" : [ "b" ], "arr2" : 2 }
{ "_id" : 1, "arr1" : [ "c" ], "arr2" : 3 }
{ "_id" : 2, "arr1" : [ "z" ], "arr2" : 1 }
{ "_id" : 2, "arr1" : [ "a" ], "arr2" : 4 }
{ "_id" : 2, "arr1" : [ "b" ], "arr2" : 2 }
{ "_id" : 2, "arr1" : [ "a" ], "arr2" : 3 }

second $unwind Output

{ "_id" : 1, "arr1" : "a", "arr2" : 1 }
{ "_id" : 1, "arr1" : "a", "arr2" : 4 }
{ "_id" : 1, "arr1" : "b", "arr2" : 2 }
{ "_id" : 1, "arr1" : "c", "arr2" : 3 }
{ "_id" : 2, "arr1" : "z", "arr2" : 1 }
{ "_id" : 2, "arr1" : "a", "arr2" : 4 }
{ "_id" : 2, "arr1" : "b", "arr2" : 2 }
{ "_id" : 2, "arr1" : "a", "arr2" : 3 }

Final $match output:

{ "_id" : 1, "arr1" : [ "a" ], "arr2" : 1 }
{ "_id" : 1, "arr1" : [ "a" ], "arr2" : 4 }
{ "_id" : 2, "arr1" : [ "a" ], "arr2" : 4 }
{ "_id" : 2, "arr1" : [ "a" ], "arr2" : 3 }
Sign up to request clarification or add additional context in comments.

2 Comments

Nice, it worked flawlessly. I also liked the way you've explained this query, showing the output of each step. Very instructive. Thank you.
glad to hear that. You are welcome. Thanks for accepting answer.

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.