I have a collection like this:
{
"_id" : ObjectId("5bd1686ba64b9206349284db"),
"type" : "Package",
"codeInstances" : [
{
"name" : "a",
"description" : "a"
},
{
"name" : "b",
"description" : "b1"
},
{
"name" : "b",
"description" : "b2"
}
]
}
{
"_id" : ObjectId("5bd16ab8a64b92068d485054"),
"type" : "Package",
"codeInstances" : [
{
"name" : "a",
"description" : "a"
},
{
"name" : "b",
"description" : "b3"
}
]
}
The following structure is what I want:
{
"name" : "b",
"description" : "b1"
},
{
"name" : "b",
"description" : "b1"
},
{
"name" : "b",
"description" : "b3"
}
I tried this aggregate operations:
db.getCollection('t_system_code').aggregate([
{"$unwind":"$codeInstances"},
{$match:{"codeInstances.name":"b","type" : "Package"}},
{"$project":{"codeInstances":1,"_id":0}}
]);
But thatundefineds not the structure I want:
{
"codeInstances" : {
"name" : "b",
"description" : "b1"
}
}
{
"codeInstances" : {
"name" : "b",
"description" : "b2"
}
}
{
"codeInstances" : {
"name" : "b",
"description" : "b3"
}
}
Help. Thank you.