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

MongoDB में सब-डॉक्यूमेंट में विशिष्ट आइटम कैसे खोजें


सब-डॉक्यूमेंट में विशिष्ट आइटम खोजने के लिए डॉट (.) नोटेशन का उपयोग करें। आइए एक संग्रह बनाते हैं:

> db.demo81.insertOne({"StudentDetails":[{"StudentName":"Carol","StudentSubject":"Java"},{ "StudentName" : "David", "StudentSubject" : "MongoDB "}]});
{ "acknowledged" : true, "insertedId" : ObjectId("5e2bf6ec71bf0181ecc4229d") }
> db.demo81.insertOne({"StudentDetails":[{"StudentName":"Mike","StudentSubject":"Python"},{ "StudentName" : "David", "StudentSubject" : "C "}]});
{ "acknowledged" : true, "insertedId" : ObjectId("5e2bf70471bf0181ecc4229e") }
> db.demo81.insertOne({"StudentDetails":[{"StudentName":"Jace","StudentSubject":"C++"},{ "StudentName" : "John", "StudentSubject" : "MySQL "}]});
{ "acknowledged" : true, "insertedId" : ObjectId("5e2bf72071bf0181ecc4229f") }

सभी दस्तावेज़ देखने के लिए:

> db.demo81.find();

आउटपुट:

{ "_id" : ObjectId("5e2bf6ec71bf0181ecc4229d"), "StudentDetails" : [
  { "StudentName" : "Carol", "StudentSubject" : "Java" },
  { "StudentName" : "David", "StudentSubject" : "MongoDB" }
]}
{ "_id" : ObjectId("5e2bf70471bf0181ecc4229e"), "StudentDetails" : [
  { "StudentName" : "Mike", "StudentSubject" : "Python" },
  { "StudentName" : "David", "StudentSubject" : "C" }
]}
{ "_id" : ObjectId("5e2bf72071bf0181ecc4229f"), "StudentDetails" : [
  { "StudentName" : "Jace", "StudentSubject" : "C++" },
  { "StudentName" : "John", "StudentSubject" : "MySQL" }
]}

सब-डॉक्यूमेंट में आइटम खोजने की क्वेरी

डॉट नोटेशन का उपयोग करके "MongoDB" विषय वाले दस्तावेज़ खोजें:

> db.demo81.find({"StudentDetails.StudentSubject":"MongoDB"});

परिणाम:

{ "_id" : ObjectId("5e2bf6ec71bf0181ecc4229d"), "StudentDetails" : [
  { "StudentName" : "Carol", "StudentSubject" : "Java" },
  { "StudentName" : "David", "StudentSubject" : "MongoDB" }
]}

महत्वपूर्ण: डॉट नोटेशन (StudentDetails.StudentSubject) नेस्टेड सरणी/ऑब्जेक्ट के भीतर फ़ील्ड तक पहुंचने का सबसे सरल तरीका है।

  1. - MongoDB में Array Elements के साथ Specific Document Fetch करना

    MongoDB में किसी specific document को fetch करने के लिए find() मेथड में dot notation का उपयोग करें। आइए documents के साथ एक collection बनाते हैं: > db.demo672.insertOne({Brand:[{CategoryName:Mobile,Name:Oppo}]}); { acknowledged : true, insertedId : ObjectId(5ea3ea9b04263e90dac943e5) } > db.dem

  1. - MongoDB Aggregation - Specific Field Value वाले Documents Fetch करना

    इसके लिए aggregate() का उपयोग करें। मान लें हमें field Age value 21 वाले documents fetch करने हैं। Collection बनाना > db.demo685.insertOne( ... { ... details: ... [ ... { Name:Chris, Age:21 }, ... { Name:David, Age:23 }, ... { Name:Bob, Age:21 } ... ] ... } ...

  1. - MongoDB में डॉक्यूमेंट और सबडॉक्यूमेंट उदाहरण के साथ अपडेट

    डॉक्यूमेंट और सबडॉक्यूमेंट दिखाने वाला सिंटैक्स: db.yourCollectionName.insertOne( { yourFieldName: yourValue, yourFieldName: [ { yourFieldName1, yourFieldName2, ... N } ] } ); आइए