In MongoDB, the $setIsSubset
aggregation pipeline operator accepts two arrays and returns true
when the first array is a subset of the second, and false
when it isn’t.
The first array is also considered a subset when it equals the second array.
$setIsSubset
accepts two arguments, both of which can be any valid expression as long as they each resolve to an array. $setIsSubset
treats the arrays as sets.
Example
Suppose we have a collection called data
with the following documents:
{ "_id" : 1, "a" : [ 1, 2, 3 ], "b" : [ 1, 2, 3 ] } { "_id" : 2, "a" : [ 1, 2, 3 ], "b" : [ 1, 2 ] } { "_id" : 3, "a" : [ 1, 2 ], "b" : [ 1, 2, 3 ] } { "_id" : 4, "a" : [ 1, 2, 3 ], "b" : [ 3, 4, 5 ] } { "_id" : 5, "a" : [ 1, 2, 3 ], "b" : [ 4, 5, 6 ] }
We can apply the $setIsSubset
operator against the a
and b
fields in those documents.
Example:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 1, 2, 3, 4, 5 ] } } },
{
$project:
{
_id: 0,
a: 1,
b: 1,
result: { $setIsSubset: [ "$a", "$b" ] }
}
}
]
)
Result:
{ "a" : [ 1, 2, 3 ], "b" : [ 1, 2, 3 ], "result" : true } { "a" : [ 1, 2, 3 ], "b" : [ 1, 2 ], "result" : false } { "a" : [ 1, 2 ], "b" : [ 1, 2, 3 ], "result" : true } { "a" : [ 1, 2, 3 ], "b" : [ 3, 4, 5 ], "result" : false } { "a" : [ 1, 2, 3 ], "b" : [ 4, 5, 6 ], "result" : false }
Nested Arrays
The $setIsSubset
operator does not descend into any nested arrays. It only evaluates top-level arrays.
Suppose our collection also contains the following documents:
{ "_id" : 6, "a" : [ 1, 2, 3 ], "b" : [ [ 1, 2, 3 ] ] } { "_id" : 7, "a" : [ 1, 2, 3 ], "b" : [ [ 1, 2 ], 3 ] }
And we apply $setIsSubset
to those two documents:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 6, 7 ] } } },
{
$project:
{
_id: 0,
a: 1,
b: 1,
result: { $setIsSubset: [ "$a", "$b" ] }
}
}
]
)
Result:
{ "a" : [ 1, 2, 3 ], "b" : [ [ 1, 2, 3 ] ], "result" : false } { "a" : [ 1, 2, 3 ], "b" : [ [ 1, 2 ], 3 ], "result" : false }
In the first document, the b
field contained an array that contained just one element – another array. In this case it was found that a
is not a subset of b
.
However, suppose we have the following documents:
{ "_id" : 8, "a" : [ [ 1, 2, 3 ] ], "b" : [ [ 1, 2, 3 ] ] } { "_id" : 9, "a" : [ [ 1, 2, 3 ] ], "b" : [ [ 1, 2 ], 3 ] }
Document 8 contains a nested array at both the a
and b
fields, and both arrays are identical.
Here’s what happens when we apply $setIsSubset
to those documents:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 8, 9 ] } } },
{
$project:
{
_id: 0,
a: 1,
b: 1,
result: { $setIsSubset: [ "$a", "$b" ] }
}
}
]
)
Result:
{ "a" : [ [ 1, 2, 3 ] ], "b" : [ [ 1, 2, 3 ] ], "result" : true } { "a" : [ [ 1, 2, 3 ] ], "b" : [ [ 1, 2 ], 3 ], "result" : false }
In the first document, a
matches b
exactly, and so the result is true
.
In the second document, a
is not a subset of b
, and so the result is false
.
Missing Fields
Applying $setIsSubset
to a non-existent field results in an error.
Consider the following documents:
{ "_id" : 10, "a" : [ 1, 2, 3 ] } { "_id" : 11, "b" : [ 1, 2, 3 ] } { "_id" : 12 }
The first document doesn’t have a b
field, the second document doesn’t have an a
field, and the third document doesn’t have either.
Here’s what happens when we apply $setIsSubset
to the a
and b
fields:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 10, 11, 12 ] } } },
{
$project:
{
_id: 0,
a: 1,
b: 1,
result: { $setIsSubset: [ "$a", "$b" ] }
}
}
]
)
Result:
Error: command failed: { "ok" : 0, "errmsg" : "both operands of $setIsSubset must be arrays. Second argument is of type: missing", "code" : 17042, "codeName" : "Location17042" } : aggregate failed : _getErrorWithCode@src/mongo/shell/utils.js:25:13 doassert@src/mongo/shell/assert.js:18:14 _assertCommandWorked@src/mongo/shell/assert.js:639:17 assert.commandWorked@src/mongo/shell/assert.js:729:16 DB.prototype._runAggregate@src/mongo/shell/db.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1058:12 @(shell):1:1
Wrong Data Type
Both operands of $setIsSubset
must be arrays. If they aren’t, an error is thrown.
Suppose our collection contains the following documents:
{ "_id" : 13, "a" : [ 1, 2, 3 ], "b" : 3 } { "_id" : 14, "a" : 3, "b" : [ 1, 2, 3 ] } { "_id" : 15, "a" : 2, "b" : 3 }
And we apply $setIsSubset
to those documents:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 13, 14, 15 ] } } },
{
$project:
{
_id: 0,
a: 1,
b: 1,
result: { $setIsSubset: [ "$a", "$b" ] }
}
}
]
)
Result:
Error: command failed: { "ok" : 0, "errmsg" : "both operands of $setIsSubset must be arrays. Second argument is of type: double", "code" : 17042, "codeName" : "Location17042" } : aggregate failed : _getErrorWithCode@src/mongo/shell/utils.js:25:13 doassert@src/mongo/shell/assert.js:18:14 _assertCommandWorked@src/mongo/shell/assert.js:639:17 assert.commandWorked@src/mongo/shell/assert.js:729:16 DB.prototype._runAggregate@src/mongo/shell/db.js:266:5 DBCollection.prototype.aggregate@src/mongo/shell/collection.js:1058:12 @(shell):1:1
Duplicate Values
The $setIsSubset
operator ignores duplicates. It also ignores the order of the elements..
Suppose we have the following documents:
{ "_id" : 16, "a" : [ 1, 1, 2, 2, 3, 3 ], "b" : [ 1, 2, 3 ] } { "_id" : 17, "a" : [ 1, 1, 2, 2, 3, 3 ], "b" : [ 1, 2 ] } { "_id" : 18, "a" : [ 1, 1, 2, 2, 3, 3 ], "b" : [ ] } { "_id" : 19, "a" : [ 3, 2, 1, 2, 3, 1 ], "b" : [ 2, 3, 1 ] } { "_id" : 20, "a" : [ 1, 3, 2, 2, 3, 1 ], "b" : [ 2, 1 ] } { "_id" : 21, "a" : [ 2, 3, 1, 2, 3, 1 ], "b" : [ ] }
Then we apply the $setIsSubset
operator to them:
db.data.aggregate(
[
{ $match: { _id: { $in: [ 16, 17, 18, 19, 20, 21 ] } } },
{
$project:
{
_id: 0,
a: 1,
b: 1,
result: { $setIsSubset: [ "$a", "$b" ] }
}
}
]
)
Result:
{ "a" : [ 1, 1, 2, 2, 3, 3 ], "b" : [ 1, 2, 3 ], "result" : true } { "a" : [ 1, 1, 2, 2, 3, 3 ], "b" : [ 1, 2 ], "result" : false } { "a" : [ 1, 1, 2, 2, 3, 3 ], "b" : [ ], "result" : false } { "a" : [ 3, 2, 1, 2, 3, 1 ], "b" : [ 2, 3, 1 ], "result" : true } { "a" : [ 1, 3, 2, 2, 3, 1 ], "b" : [ 2, 1 ], "result" : false } { "a" : [ 2, 3, 1, 2, 3, 1 ], "b" : [ ], "result" : false }