Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> MongoDB

MongoDB में दो लेवल गहरे रिकॉर्ड को कैसे खोजें?


दो लेवल गहरे 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 } }

  1. - MongoDB में रिकॉर्ड कैसे अपडेट करें?

    रिकॉर्ड अपडेट करने के लिए, आपको _id के आधार पर अपडेट करना होगा। आइए दस्तावेज़ों के साथ एक संग्रह बनाएं: > db.demo458.insertOne( {_id:101,"Name":"David" } ); { "acknowledged" : true, "insertedId" : 101 } > db.demo458.insertOne( {_id:102,"Name":

  1. - MongoDB में कैसे पता करें कि एलिमेंट दस्तावेज़ में मौजूद है?

    MongoDB दस्तावेज़ में एलिमेंट मौजूद है या नहीं जांचने के लिए MongoDB $exists का उपयोग करें। नमूना डेटा बनाना db.demo497.insertOne({details:[{}]}); सभी दस्तावेज़ देखना db.demo497.find(); { details : [ { Name : Chris }, { Name : Bob } ] } { details : [ { Name : Carol } ] } { details : [ { } ] } द

  1. - MongoDB में Sub-documents पर Find Query Execute करना

    Sub-documents के लिए dot notation का उपयोग करें। आइए पहले documents के साथ एक collection बनाते हैं: > db.demo537.insertOne({details:{SubjectName:MongoDB}}); { acknowledged : true, insertedId : ObjectId(5e8c8a10ef4dcbee04fbbc05) } > db.demo537.insertOne({details:{SubjectName:MySQL}}); { acknowl