दो लेवल गहरे MongoDB रिकॉर्ड को खोजने के लिए MongoDB $where में लूप करें। आइए दस्तावेज़ों के साथ एक संग्रह बनाएं:
> db.demo468.insertOne( ... { ... "_id" : new ObjectId(), ... "FirstPosition" : { ... "StudentName" : "Chris", ... "StudentAge" : 23 ... }, ... "SecondPosition" : { ... "StudentName" : "David", ... "StudentAge" : 20 ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e804e2fb0f3fa88e2279069") } > db.demo468.insertOne( ... { ... "_id" : new ObjectId(), ... "FirstPosition" : { ... "StudentName" : "Carol", ... "StudentAge" : 21 ... }, ... "SecondPosition" : { ... "StudentName" : "John", ... "StudentAge" : 22 ... } ... } ... ); { "acknowledged" : true, "insertedId" : ObjectId("5e804fb0b0f3fa88e227906a") }
सभी दस्तावेज़ प्रदर्शित करें
> db.demo468.find();
आउटपुट:
{ "_id" : ObjectId("5e804e2fb0f3fa88e2279069"), "FirstPosition" : { "StudentName" : "Chris", "StudentAge" : 23 }, "SecondPosition" : { "StudentName" : "David", "StudentAge" : 20 } } { "_id" : ObjectId("5e804fb0b0f3fa88e227906a"), "FirstPosition" : { "StudentName" : "Carol", "StudentAge" : 21 }, "SecondPosition" : { "StudentName" : "John", "StudentAge" : 22 } }
दो लेवल गहरे रिकॉर्ड खोजने की क्वेरी
> db.demo468.find({ ... $where: function() { ... for (var i in this) { ... if (this[i]["StudentName"] == "John") { ... return true; ... } ... } ... return false; ... } ... })
आउटपुट
{ "_id" : ObjectId("5e804fb0b0f3fa88e227906a"), "FirstPosition" : { "StudentName" : "Carol", "StudentAge" : 21 }, "SecondPosition" : { "StudentName" : "John", "StudentAge" : 22 } }