View a markdown version of this page

$addToSet - Amazon DocumentDB

$addToSet

The $addToSet aggregation operator returns an array of unique values from a specified expression for each group. It is used within the $group stage to accumulate distinct values, automatically eliminating duplicates.

Parameters

  • expression: The expression to evaluate for each document in the group.

Example (MongoDB Shell)

The following example demonstrates using the $addToSet operator to collect unique cities where orders were placed for each customer.

Create sample documents

db.orders.insertMany([ { _id: 1, customer: "Alice", city: "Seattle", amount: 100 }, { _id: 2, customer: "Alice", city: "Portland", amount: 150 }, { _id: 3, customer: "Bob", city: "Seattle", amount: 200 }, { _id: 4, customer: "Alice", city: "Seattle", amount: 75 }, { _id: 5, customer: "Bob", city: "Boston", amount: 300 } ]);

Query example

db.orders.aggregate([ { $group: { _id: "$customer", cities: { $addToSet: "$city" } } } ]);

Output

[ { _id: 'Bob', cities: [ 'Seattle', 'Boston' ] }, { _id: 'Alice', cities: [ 'Seattle', 'Portland' ] } ]

Window operator usage example (MongoDB Shell)

New from version 8.0.2.

The $addToSet operator can also be used as a window operator in the $setWindowFields stage. In this context, it returns an array of the unique values of the specified expression for the documents in each window. You specify the operator under the output field, and optionally define the window boundaries with a window document.

Note

When used as a window operator in $setWindowFields, $addToSet is limited to 100 MB of intermediate data. An operation that exceeds this limit returns an error.

Create sample documents

db.visits.insertMany([ { _id: 1, customer: "Alice", day: 1, city: "Seattle" }, { _id: 2, customer: "Alice", day: 2, city: "Portland" }, { _id: 3, customer: "Alice", day: 3, city: "Seattle" }, { _id: 4, customer: "Bob", day: 1, city: "Boston" }, { _id: 5, customer: "Bob", day: 2, city: "Boston" } ]);

Query example

The following example partitions the documents by customer, sorts each partition by day, and returns the set of unique city values from the start of the partition through the current document.

db.visits.aggregate([ { $setWindowFields: { partitionBy: "$customer", sortBy: { day: 1 }, output: { citiesSoFar: { $addToSet: "$city", window: { documents: ["unbounded", "current"] } } } } } ]);

Output

[ { "_id": 1, "customer": "Alice", "day": 1, "city": "Seattle", "citiesSoFar": [ "Seattle" ] }, { "_id": 2, "customer": "Alice", "day": 2, "city": "Portland", "citiesSoFar": [ "Portland", "Seattle" ] }, { "_id": 3, "customer": "Alice", "day": 3, "city": "Seattle", "citiesSoFar": [ "Portland", "Seattle" ] }, { "_id": 4, "customer": "Bob", "day": 1, "city": "Boston", "citiesSoFar": [ "Boston" ] }, { "_id": 5, "customer": "Bob", "day": 2, "city": "Boston", "citiesSoFar": [ "Boston" ] } ]

Each document is augmented with citiesSoFar, the set of unique city values within its partition from the start through the current document. As with the accumulator form, the order of values in the returned array is not guaranteed.

Code examples

To view a code example for using the $addToSet operator, choose the tab for the language that you want to use. The following examples show both accumulator usage (in $group) and window operator usage (in $setWindowFields):

Node.js
const { MongoClient } = require('mongodb'); async function example() { const client = await MongoClient.connect('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false'); const db = client.db('test'); // Accumulator usage: collect unique values per group const orders = db.collection('orders'); await orders.insertMany([ { _id: 1, customer: "Alice", city: "Seattle", amount: 100 }, { _id: 2, customer: "Alice", city: "Portland", amount: 150 }, { _id: 3, customer: "Bob", city: "Seattle", amount: 200 }, { _id: 4, customer: "Alice", city: "Seattle", amount: 75 }, { _id: 5, customer: "Bob", city: "Boston", amount: 300 } ]); const accumulatorResult = await orders.aggregate([ { $group: { _id: "$customer", cities: { $addToSet: "$city" } } } ]).toArray(); console.log('Accumulator result:', accumulatorResult); // Window operator usage: running set of unique values within each partition const visits = db.collection('visits'); await visits.insertMany([ { _id: 1, customer: "Alice", day: 1, city: "Seattle" }, { _id: 2, customer: "Alice", day: 2, city: "Portland" }, { _id: 3, customer: "Alice", day: 3, city: "Seattle" }, { _id: 4, customer: "Bob", day: 1, city: "Boston" }, { _id: 5, customer: "Bob", day: 2, city: "Boston" } ]); const windowResult = await visits.aggregate([ { $setWindowFields: { partitionBy: "$customer", sortBy: { day: 1 }, output: { citiesSoFar: { $addToSet: "$city", window: { documents: ["unbounded", "current"] } } } } } ]).toArray(); console.log('Window result:', windowResult); await client.close(); } example();
Python
from pymongo import MongoClient def example(): client = MongoClient('mongodb://<username>:<password>@<cluster-endpoint>:27017/?tls=true&tlsCAFile=global-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false') db = client['test'] # Accumulator usage: collect unique values per group orders = db['orders'] orders.insert_many([ { '_id': 1, 'customer': 'Alice', 'city': 'Seattle', 'amount': 100 }, { '_id': 2, 'customer': 'Alice', 'city': 'Portland', 'amount': 150 }, { '_id': 3, 'customer': 'Bob', 'city': 'Seattle', 'amount': 200 }, { '_id': 4, 'customer': 'Alice', 'city': 'Seattle', 'amount': 75 }, { '_id': 5, 'customer': 'Bob', 'city': 'Boston', 'amount': 300 } ]) accumulator_result = list(orders.aggregate([ { '$group': { '_id': '$customer', 'cities': { '$addToSet': '$city' } } } ])) print('Accumulator result:', accumulator_result) # Window operator usage: running set of unique values within each partition visits = db['visits'] visits.insert_many([ { '_id': 1, 'customer': 'Alice', 'day': 1, 'city': 'Seattle' }, { '_id': 2, 'customer': 'Alice', 'day': 2, 'city': 'Portland' }, { '_id': 3, 'customer': 'Alice', 'day': 3, 'city': 'Seattle' }, { '_id': 4, 'customer': 'Bob', 'day': 1, 'city': 'Boston' }, { '_id': 5, 'customer': 'Bob', 'day': 2, 'city': 'Boston' } ]) window_result = list(visits.aggregate([ { '$setWindowFields': { 'partitionBy': '$customer', 'sortBy': { 'day': 1 }, 'output': { 'citiesSoFar': { '$addToSet': '$city', 'window': { 'documents': ['unbounded', 'current'] } } } } } ])) print('Window result:', window_result) client.close() example()