$filter - Amazon DocumentDB
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

$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 the input array within the cond expression.

  • 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:

Node.js
const { MongoClient } = require('mongodb'); async function example() { const uri = 'mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'; const client = new MongoClient(uri); try { await client.connect(); const db = client.db('test'); const collection = db.collection('orders'); const result = await collection.aggregate([ { $project: { customer: 1, paidItems: { $filter: { input: "$items", as: "item", cond: { $gt: ["$$item.price", 15] } } } } } ]).toArray(); console.log(JSON.stringify(result, null, 2)); } catch (error) { console.error('Error:', error); } finally { await client.close(); } } example();
Python
from pymongo import MongoClient from pprint import pprint def example(): uri = 'mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false' with MongoClient(uri) as client: db = client.test collection = db.orders result = list(collection.aggregate([ { '$project': { 'customer': 1, 'paidItems': { '$filter': { 'input': '$items', 'as': 'item', 'cond': { '$gt': ['$$item.price', 15] } } } } } ])) for doc in result: pprint(doc) example()