Reputation: 513
I have this document structure
{
"_id" : ObjectId("63b6757dfa46345e268b7ba5"),
"name" : "Blue Skinny Jeans",
"sizes" : [45,46],
"textile" : "Wool",
"merchantCategory" : "Jeans",
"tags" : ["Tag 1","Tag 2"],
"sku" : "12161-2753930-5053177208071",
"manufacturer" : "Hugo Boss"
}
/* 1 */
{
"_id" : ObjectId("53b6757dfa46345e268b7ba4"),
"name" : "Blue Skinny Jeans",
"sizes" : [40,41],
"textile" : "Polyester",
"merchantCategory" : "Jeans",
"tags" : ["Tag 2","Tag 3"],
"sku" : "12161-2753930-5053177208071",
"manufacturer" : "Hugo Boss"
}
Is it possible to build a query which "groups" the documents by name and merge the fields which are not equal (sizes,tags,merchantCategory,textile)
Upvotes: 3
Views: 194
Reputation: 3297
This might work,
coll.aggregate({"$group":{"_id":"$name",
"sizes":{"$addToSet":"$sizes"},
"tags":{"$addToSet":"$tags"},
..
..
})
Upvotes: 1