Docs Menu
Docs Home
/
MongoDB Manual
/ / /

$setEquals (aggregation)

On this page

  • Definition
  • Behavior
  • Example
$setEquals

Compares two or more arrays and returns true if they have the same distinct elements and false otherwise.

$setEquals has the following syntax:

{ $setEquals: [ <expression1>, <expression2>, ... ] }

The arguments can be any valid expression as long as they each resolve to an array. For more information on expressions, see Expressions.

$setEquals performs set operation on arrays, treating arrays as sets. If an array contains duplicate entries, $setEquals ignores the duplicate entries. $setEquals ignores the order of the elements.

If a set contains a nested array element, $setEquals does not descend into the nested array but evaluates the array at top-level.

Example
Result

{ $setEquals: [ [ "a", "b", "a" ], [ "b", "a" ] ] }

true

{ $setEquals: [ [ "a", "b" ], [ [ "a", "b" ] ] ] }

false

Consider a bakeryOrders collection with the following documents:

db.bakeryOrders.insertMany( [
{ _id: 0, cakes: ["chocolate", "vanilla"], cupcakes: ["chocolate", "vanilla"] },
{ _id: 1, cakes: ["chocolate", "vanilla"], cupcakes: ["vanilla", "chocolate"] },
{ _id: 2, cakes: ["chocolate", "chocolate"], cupcakes: ["chocolate"] },
{ _id: 3, cakes: ["vanilla"], cupcakes: ["chocolate"] },
{ _id: 4, cakes: ["vanilla"], cupcakes: [] }
] )

The following operation uses the $setEquals operator to determine if the cakes array and the cupcakes array in each order contain the same flavors:

db.bakeryOrders.aggregate(
[
{
$project: {
_id: 0,
cakes: 1,
cupcakes: 1,
sameFlavors: { $setEquals: [ "$cakes", "$cupcakes" ] }
}
}
] )

Note

$project

The $project stage specifies which fields are included in the output documents. In this example, the $project stage:

  • Excludes the _id field from the output.

  • Includes the cakes and cupcakes fields in the output.

  • Outputs the result of the $setEquals operator in a new field called sameFlavors.

The operation returns the following results:

{
cakes: [ "chocolate", "vanilla" ],
cupcakes: [ "chocolate", "vanilla" ],
sameFlavors: true
},
{
cakes: [ "chocolate", "vanilla" ],
cupcakes: [ "vanilla", "chocolate" ],
sameFlavors: true
},
{
cakes: [ "chocolate", "chocolate" ],
cupcakes: [ "chocolate" ],
sameFlavors: true
},
{
cakes: [ "vanilla" ],
cupcakes: [ "chocolate" ],
sameFlavors: false
},
{
cakes: [ "vanilla" ],
cupcakes: [],
sameFlavors: false
}

Back

$setDifference