$filter
The $filter operator in Amazon DocumentDB is used to apply a filter expression to each element of an array and return an array containing only the elements that match the specified condition. This operator is useful when you need to perform complex filtering operations on array fields within your documents.
Parameters
-
input: The array field to filter. -
as: The variable name to use for each element of theinputarray within thecondexpression. -
cond: The boolean expression that determines whether a given element should be included in the output array.
Example (MongoDB Shell)
The following example demonstrates how to use the $filter operator to project each order's customer and create a new array field paidItems containing only the items from the items array where the price is greater than 15. Essentially, it filters each order’s items to include only products that cost more than 15.
Create sample documents
db.orders.insertMany([ { _id: 1, customer: "abc123", items: [ { name: "Product A", price: 10, qty: 2 }, { name: "Product B", price: 20, qty: 1 } ]}, { _id: 2, customer: "def456", items: [ { name: "Product C", price: 5, qty: 3 }, { name: "Product D", price: 15, qty: 4 } ]}, { _id: 3, customer: "ghi789", items: [ { name: "Product E", price: 8, qty: 3 }, { name: "Product F", price: 12, qty: 1 } ]} ]);
Query example
db.orders.aggregate([ { $project: { customer: 1, paidItems: { $filter: { input: "$items", as: "item", cond: { $gt: ["$$item.price", 15] } } } } } ]).pretty();
Output
[
{
_id: 1,
customer: 'abc123',
paidItems: [ { name: 'Product B', price: 20, qty: 1 } ]
},
{ _id: 2, customer: 'def456', paidItems: [] },
{ _id: 3, customer: 'ghi789', paidItems: [] }
]
Code examples
To view a code example for using the $filter command, choose the tab for the language that you want to use: