In mongodb, I have the data structure like this:
{
"data" : [
{
"car" : [
{
"model" : "aaa",
},
{
"model" : "bbb",
},
{
"model" : "ccc",
}
],
"user" : {
"id" : "123"
}
}
],
}
And I want to group and count the car model field. So first I tried the unwind method like this:
.aggregate([
{ $project: {"data.car.model":1, "data.user.id":1} },
{ $unwind: {
path: "$data.car",
preserveNullAndEmptyArrays: true
} },
{ $group: {
_id: "$data.car.model",
count: { $sum: 1 }
} }
])
But the result is:
{
"_id" : [ ["aaa", "bbb", "ccc"] ],
"count" : 1.0
}
which is not what I wanted since the model didn't unwind. The result I'm hoping is:
[{_id:"aaa",count:1}, {_id:"bbb",count:1}, {_id:"ccc", count:1}]
Then by looked at mongodb unwind array nested inside an array of documents, I tried the $project the nested array:
.aggregate([
{ $project: {"car":"$data.car.model", "user":"$data.user.id"} },
])
But the result is:
{
"car" : [ ["aaa", "bbb", "ccc"] ],
"user" : ["123"]
}
As you can see, the car and user was embedded in an array, which I still can't apply the $unwind method. However, I hope it should be like:
{
"car" : ["aaa", "bbb", "ccc"],
"user" : "123"
}
The mongo I'm using is 3.2, I searched a lot on the web but didn't find the answer. Is there possible to do so?