Same array elements वाले document को find करने के लिए find() में $all का उपयोग करें। $all operator उन documents को select करता है जहां field का value एक array है जिसमें सभी specified elements मौजूद हैं।
Collection बनाना
> db.demo543.insertOne({id:101, subject:["MySQL", "Java" ,"C","Python"]});
{ "acknowledged" : true, "insertedId" : ObjectId("5e8e1b2f9e5f92834d7f05c9") }
> db.demo543.insertOne({id:102, subject:["MySQL", "MongoDB" ,"SQL Server"]});
{ "acknowledged" : true, "insertedId" : ObjectId("5e8e1b2f9e5f92834d7f05ca") }
सभी Documents Display करना
> db.demo543.find();
Output:
{ "_id" : ObjectId("5e8e1b2f9e5f92834d7f05c9"), "id" : 101, "subject" : [ "MySQL", "Java", "C", "Python" ] }
{ "_id" : ObjectId("5e8e1b2f9e5f92834d7f05ca"), "id" : 102, "subject" : [ "MySQL", "MongoDB", "SQL Server" ] }
Same Array Elements Match करने का Query
> db.demo543.find({
... "subject": { $all: [ "MySQL", "MongoDB", "SQL Server"], $size: 3 }
... })
Output:
{ "_id" : ObjectId("5e8e1b2f9e5f92834d7f05ca"), "id" : 102, "subject" : [ "MySQL", "MongoDB", "SQL Server" ] }
व्याख्या: $all सुनिश्चित करता है कि सभी specified elements array में मौजूद हों। $size: 3 सुनिश्चित करता है कि array में exactly 3 elements हों (कोई extra element नहीं)। इससे exact array match होता है।