Suppose you have the following documents in my collection:
{
"_id" : ObjectId("5ddb10ff6e25e6fec648df70"),
"shapes" : [
{
"color" : [
"blue",
"red"
],
"shape" : "square"
},
{
"color" : [
"red",
"green"
],
"shape" : "circle"
}
],
"name" : "customer1"
}
I have the following query:
db.customers
.aggregate([
{ $match: { "shapes.color": "red" } },
{
$project: {
shapes: {
$filter: {
input: "$shapes",
as: "shape",
cond: { $eq: ["$$shape.color", "red"] }
}
}
}
}
])
.pretty();
Returns matched document:
{ "_id" : ObjectId("5ddb10ff6e25e6fec648df70"), "shapes" : [ ] }
But I was expecting this:
{
"_id" : ObjectId("5ddb10629f3f949e3feddf19"),
"shapes" : [
{
"color" : ["red"],
"shape" : "square"
},
{
"color" : ["red"],
"shape" : "circle"
}
]
}
How do I accomplish this?