For this, use $setIsSubset in MongoDB. Let us create a collection with documents −
> db.demo407.insertOne( ... { ... Name:"Chris", ... "details" : [ ... { ... id:100 ... }, ... { ... id:110 ... }, ... { ... id:130 ... } ... ] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e70dffe15dc524f70227677") }
Display all documents from a collection with the help of find() method −
> db.demo407.find().pretty();
This will produce the following output −
{ "_id" : ObjectId("5e70dffe15dc524f70227677"), "Name" : "Chris", "details" : [ { "id" : 100 }, { "id" : 110 }, { "id" : 130 } ]
Following is the query to use arrays as filters by querying subdocuments −
> db.demo407.aggregate([ ... {$match: { }}, ... {$project: { ... "details": { ... $filter: { ... input: "$details", ... as: "output", ... cond: { $setIsSubset: [["$$output.id"],[100, 130]] } ... } ... } ... }}])
This will produce the following output −
{ "_id" : ObjectId("5e70dffe15dc524f70227677"), "details" : [ { "id"