|
Tested as working in MongoDB 3.2. Note that $filter is not available for version of MongoDB prior to 3.2.0
> db.foo.find().pretty()
|
{
|
"_id" : 0,
|
"items" : [
|
{
|
"item_id" : 43,
|
"quantity" : 2,
|
"price" : 10
|
},
|
{
|
"item_id" : 2,
|
"quantity" : 1,
|
"price" : 240
|
}
|
]
|
}
|
{
|
"_id" : 1,
|
"items" : [
|
{
|
"item_id" : 23,
|
"quantity" : 3,
|
"price" : 110
|
},
|
{
|
"item_id" : 103,
|
"quantity" : 4,
|
"price" : 5
|
},
|
{
|
"item_id" : 38,
|
"quantity" : 1,
|
"price" : 300
|
}
|
]
|
}
|
{
|
"_id" : 2,
|
"items" : [
|
{
|
"item_id" : 4,
|
"quantity" : 1,
|
"price" : 23
|
}
|
]
|
}
|
>
|
>
|
> db.foo.aggregate([
|
... {
|
... $project: {
|
... items: {
|
... $filter: {
|
... input: "$items",
|
... as: "item",
|
... cond: { $gte: [ "$$item.price", 100 ] }
|
... }
|
... }
|
... }
|
... }
|
... ])
|
{ "_id" : 0, "items" : [ { "item_id" : 2, "quantity" : 1, "price" : 240 } ] }
|
{ "_id" : 1, "items" : [ { "item_id" : 23, "quantity" : 3, "price" : 110 }, { "item_id" : 38, "quantity" : 1, "price" : 300 } ] }
|
{ "_id" : 2, "items" : [ ] }
|
|