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

MongoDB में सरणी के अंदर दस्तावेज़ से विशिष्ट फील्ड प्रोजेक्ट करना

स्थितीय ($) ऑपरेटर का उपयोग

MongoDB में, किसी सरणी के अंदर नेस्टेड दस्तावेज़ से विशिष्ट फील्ड प्रोजेक्ट करने के लिए स्थितीय ($) ऑपरेटर का उपयोग किया जाता है। यह क्वेरी मानदंड से मेल खाने वाले सरणी के पहले तत्व को प्रोजेक्ट करता है।

उदाहरण: संग्रह और दस्तावेज़ बनाना

> db.projectSpecificFieldDemo.insertOne(
... {
...   "UniqueId": 101,
...   "StudentDetails" : [
...     {"StudentName" : "Chris", "StudentCountryName " : "US"},
...     {"StudentName" : "Robert", "StudentCountryName" : "UK"}
...   ]
... }
... );
{ "acknowledged" : true, "insertedId" : ObjectId("5ca27aeb6304881c5ce84ba2") }
> db.projectSpecificFieldDemo.insertOne(
... {
...   "UniqueId": 102,
...   "StudentDetails" : [
...     {"StudentName" : "Robert", "StudentCountryName " : "UK"},
...     {"StudentName" : "David", "StudentCountryName" : "AUS"}
...   ]
... }
... );
{ "acknowledged" : true, "insertedId" : ObjectId("5ca27b106304881c5ce84ba3") }

सभी दस्तावेज़ देखना

> db.projectSpecificFieldDemo.find().pretty();

आउटपुट:

{ "_id" : ObjectId("5ca27aeb6304881c5ce84ba2"), "UniqueId" : 101, "StudentDetails" : [ { "StudentName" : "Chris", "StudentCountryName " : "US" }, { "StudentName" : "Robert", "StudentCountryName" : "UK" } ] }
{ "_id" : ObjectId("5ca27b106304881c5ce84ba3"), "UniqueId" : 102, "StudentDetails" : [ { "StudentName" : "Robert", "StudentCountryName " : "UK" }, { "StudentName" : "David", "StudentCountryName" : "AUS" } ] }

स्थितीय ऑपरेटर के साथ प्रोजेक्शन

UniqueId 101 वाले दस्तावेज़ में, StudentName "Chris" वाले सरणी तत्व को प्रोजेक्ट करें:

> var myDocument = { UniqueId : 101, 'StudentDetails.StudentName' : 'Chris' };
> var myProjection= {'StudentDetails.$': 1 };
> db.projectSpecificFieldDemo.find(myDocument , myProjection).pretty();

आउटपुट:

{ "_id" : ObjectId("5ca27aeb6304881c5ce84ba2"), "StudentDetails" : [ { "StudentName" : "Chris", "StudentCountryName " : "US" } ] }

महत्वपूर्ण: स्थितीय ऑपरेटर $ केवल क्वेरी मानदंड से मेल खाने वाले **पहले** सरणी तत्व को प्रोजेक्ट करता है। यदि आपको सभी मेल खाने वाले तत्वों की आवश्यकता है, तो एग्रीगेशन पाइपलाइन में $filter या $map का उपयोग करें।

  1. - MongoDB में Document से Array Value Delete करना

    Array values delete करने के लिए MongoDB में $pull का उपयोग करें। $pull operator existing array से उन सभी instances को remove करता है जो specified condition से match करते हैं। Collection बनाना > db.demo535.insertOne( ... { ... studentId : 101, ... studentName : Chris, ... ListOfMailIds : [

  1. - MongoDB में Document के Fields Access करना

    Document में fields access करने के लिए simply find() का उपयोग करें। Collection बनाना > db.demo565.insertOne( ... { ... id:101, ... Name:David, ... CountryName:US ... } ... ); { acknowledged : true, insertedId : ObjectId(5e90896739cfeaaf0b97b577) } > db.demo565.insertOne( ... { ... id:

  1. - MongoDB डॉक्यूमेंट से विशिष्ट मान फ़िल्टर करें

    विशिष्ट मान फ़िल्टर करने के लिए MongoDB में $filter का उपयोग करें। आइए एक कलेक्शन बनाते हैं: > db.demo751.insertOne( { _id: 101, details: [ { Name: Robert, id:110,Age:21}, { Name: Rae, id:110,Age:22}, {Name: Ralph, id:116,Age:23} ] } ); { acknowledged : true, insertedId : 101 } find() से सभी डॉक