$setIsSubset
The $setIsSubset operator in Amazon DocumentDB is used to determine if a set of values is a subset of another set. It is useful for performing set-based comparisons and operations on array fields.
Parameters
-
field: The field to apply the$setIsSubsetoperator to. -
set: The set to compare the field against.
Example (MongoDB Shell)
The following example demonstrates the usage of the $setIsSubset operator to check if the tags field is a subset of the specified set.
Create sample documents
db.products.insertMany([ { _id: 1, name: "Product A", tags: ["tag1", "tag2", "tag3"] }, { _id: 2, name: "Product B", tags: ["tag1", "tag2"] }, { _id: 3, name: "Product C", tags: ["tag2", "tag3"] } ]);
Query example
db.products.find({ $expr: { $setIsSubset: [["tag1", "tag2"], "$tags"] } })
*Note:* $setIsSubset is an aggregation operator and cannot be used directly in find() queries. In this example, $expr is used with find() to bridge the gap between query operators and aggregation expressions.
Output
[
{ "_id" : 1, "name" : "Product A", "tags" : [ "tag1", "tag2", "tag3" ] },
{ "_id" : 2, "name" : "Product B", "tags" : [ "tag1", "tag2" ] }
]
The query returns the documents where the tags field is a subset of the set ["tag1", "tag2"].
Code examples
To view a code example for using the $setIsSubset command, choose the tab for the language that you want to use: