$setEquals - 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).

$setEquals

The $setEquals operator in Amazon DocumentDB is used to determine if two sets are equal. It compares two arrays and returns true if they contain the same distinct elements, regardless of their order.

Parameters

  • expression1: The first array to compare.

  • expression2: The second array to compare.

Example (MongoDB Shell)

The following example demonstrates the usage of the $setEquals operator to compare two sets of values.

Create sample documents

db.collection.insertMany([ { _id: 1, fruits: ["apple", "banana", "cherry"] }, { _id: 2, fruits: ["banana", "apple", "cherry"] }, { _id: 3, fruits: ["apple", "banana", "orange"] } ])

Query example

db.collection.find({ $expr: { $setEquals: ["$fruits", ["apple", "banana", "cherry"]] } })

Output

{ "_id" : 1, "fruits" : [ "apple", "banana", "cherry" ] } { "_id" : 2, "fruits" : [ "banana", "apple", "cherry" ] }

The query uses the $setEquals operator to compare the fruits field of each document with the array ["apple", "banana", "cherry"]. The documents where the fruits field is equal to the comparison array are returned.

Code examples

To view a code example for using the $setEquals command, choose the tab for the language that you want to use:

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'); const collection = db.collection('mycollection'); // Insert sample documents await collection.insertMany([ { _id: 1, fruits: ["apple", "banana", "cherry"] }, { _id: 2, fruits: ["banana", "apple", "cherry"] }, { _id: 3, fruits: ["apple", "banana", "orange"] } ]); // Query using $setEquals const result = await collection.find({ $expr: { $setEquals: ["$fruits", ["apple", "banana", "cherry"]] } }).toArray(); console.log(result); 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'] collection = db['mycollection'] # Insert sample documents collection.insert_many([ {"_id": 1, "fruits": ["apple", "banana", "cherry"]}, {"_id": 2, "fruits": ["banana", "apple", "cherry"]}, {"_id": 3, "fruits": ["apple", "banana", "orange"]} ]) # Query using $setEquals result = list(collection.find({ "$expr": { "$setEquals": ["$fruits", ["apple", "banana", "cherry"]] } })) print(result) client.close() example()