I have these documents in a collection :
{topic : "a",
messages : [ObjectId("21312321321323"),ObjectId("34535345353"),...]
},
{topic : "b,
messages : [ObjectId("1233232323232"),ObjectId("6556565656565"),...]
}
Is there a posibility to get a result with the combination of messages fields ? I like to get this for example :
{[
ObjectId(""),ObjectId(""),ObjectId(""),ObjectId("")
]}
I thought that this was possible with MapReduce but in my case the documents doesn’t have anything in common. Right now I’m doing this in the backend using javascript and loops, but i think that this isn’t the best option. Thanks.
You could use the
$groupoperator in the Aggregation Framework. To use the Aggregation Framework you will want to be sure you’re running on MongoDB 2.2 or newer, of course.If used with
$pushyou will get all the lists of messages concatenated together.If used with
$addToSetyou will get only the distinct values.And if you want to filter down the candidate documents first, you can use
$match.