इसके लिए $elemMatch का उपयोग करें, जिसका उपयोग नेस्टेड ऑब्जेक्ट क्वेरी करने के लिए किया जाता है। आइए दस्तावेज़ों के साथ एक संग्रह बनाएं:
> db.demo444.insertOne( ... { ... "Information": [{ ... id:1, ... Name:"Chris" ... }] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e78ea87bbc41e36cc3caebf") } > db.demo444.insertOne( ... { ... "Information": [{ ... id:2, ... Name:"David" ... }] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e78ea87bbc41e36cc3caec0") } > db.demo444.insertOne( ... { ... "Information": [{ ... id:3, ... Name:"Bob" ... }] ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e78ea88bbc41e36cc3caec1") }
सभी दस्तावेज़ प्रदर्शित करें
> db.demo444.find();
आउटपुट:
{ "_id" : ObjectId("5e78ea87bbc41e36cc3caebf"), "Information" : [ { "id" : 1, "Name" : "Chris" } ] } { "_id" : ObjectId("5e78ea87bbc41e36cc3caec0"), "Information" : [ { "id" : 2, "Name" : "David" } ] } { "_id" : ObjectId("5e78ea88bbc41e36cc3caec1"), "Information" : [ { "id" : 3, "Name" : "Bob" } ] }
मल्टीकी इंडेक्स के साथ क्वेरी सुधारने की क्वेरी
> db.demo444.find({ ... "Information":{ ... $elemMatch:{ ... id:2, ... Name:"David" ... } ... } ... })
आउटपुट
{ "_id" : ObjectId("5e78ea87bbc41e36cc3caec0"), "Information" : [ { "id" : 2, "Name" : "David" } ] }