MongoDB में अलग क्रम में मानों के साथ सटीक ऐरे मिलान
अलग क्रम में मानों के साथ सटीक ऐरे मिलान खोजने के लिए, आप $all ऑपरेटर का उपयोग कर सकते हैं। आइए दस्तावेज़ों के साथ एक कलेक्शन बनाते हैं। निम्नलिखित क्वेरी है:
>db.exactMatchArrayDemo.insertOne({"StudentName":"David","StudentAge":22,"StudentGameScores":[45,78,98]});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9c94702d6669774125246c") }
>db.exactMatchArrayDemo.insertOne({"StudentName":"Chris","StudentAge":23,"StudentGameScores":[45,78]});
{ "acknowledged" : true, "insertedId" : ObjectId("5c9c94a42d6669774125246d") }
find() मेथड से सभी दस्तावेज़ प्रदर्शित करना
> db.exactMatchArrayDemo.find().pretty();
यह निम्नलिखित आउटपुट देगा:
{ "_id" : ObjectId("5c9c94702d6669774125246c"), "StudentName" : "David", "StudentAge" : 22, "StudentGameScores" : [ 45, 78, 98 ] }
{ "_id" : ObjectId("5c9c94a42d6669774125246d"), "StudentName" : "Chris", "StudentAge" : 23, "StudentGameScores" : [ 45, 78 ] }
सटीक ऐरे मिलान खोजने का क्वेरी
> db.exactMatchArrayDemo.find({ "StudentGameScores": { "$size" : 2, "$all": [ 78, 45 ] } }).pretty();
यह निम्नलिखित आउटपुट देगा (आकार 2 और दोनों मान 78, 45 वाले):
क्रम मायने नहीं रखता लेकिन आकार 3 हो
> db.exactMatchArrayDemo.find({ "StudentGameScores": { "$size" : 3, "$all": [ 78, 45 ] } }).pretty();यह निम्नलिखित आउटपुट देगा (आकार 3 और दोनों मान 78, 45 वाले):
{ "_id" : ObjectId("5c9c94702d6669774125246c"), "StudentName" : "David", "StudentAge" : 22, "StudentGameScores" : [ 45, 78, 98 ] }