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

MongoDB में किसी भी शर्त के बिना सरणी से सभी एलिमेंट कैसे खींचें?

आप इसके लिए $set ऑपरेटर का उपयोग कर सकते हैं। पहले दस्तावेजों के साथ एक संग्रह बनाते हैं:

> db.pullAllElementDemo.insertOne({
  "StudentId":101,
  "StudentDetails" : [
    { "StudentName": "Carol", "StudentAge":21, "StudentCountryName":"US" },
    { "StudentName": "Chris", "StudentAge":24, "StudentCountryName":"AUS" }
  ]
});
{ "acknowledged" : true, "insertedId" : ObjectId("5ccdd9c8685b30d09a7111e4") }
> db.pullAllElementDemo.insertOne({
  "StudentId":102,
  "StudentDetails" : [
    { "StudentName": "Robert", "StudentAge":27, "StudentCountryName":"UK" },
    { "StudentName": "David", "StudentAge":23, "StudentCountryName":"US" }
  ]
});
{ "acknowledged" : true, "insertedId" : ObjectId("5ccdd9f7685b30d09a7111e5") }

संग्रह से सभी दस्तावेजों को find() विधि की सहायता से प्रदर्शित करने के लिए क्वेरी:

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

यह निम्नलिखित आउटपुट उत्पन्न करेगा:

{ "_id" : ObjectId("5ccdd9c8685b30d09a7111e4"), "StudentId" : 101, "StudentDetails" : [ { "StudentName" : "Carol", "StudentAge" : 21, "StudentCountryName" : "US" }, { "StudentName" : "Chris", "StudentAge" : 24, "StudentCountryName" : "AUS" } ] }
{ "_id" : ObjectId("5ccdd9f7685b30d09a7111e5"), "StudentId" : 102, "StudentDetails" : [ { "StudentName" : "Robert", "StudentAge" : 27, "StudentCountryName" : "UK" }, { "StudentName" : "David", "StudentAge" : 23, "StudentCountryName" : "US" } ] }

MongoDB में किसी भी शर्त के बिना सरणी से सभी एलिमेंट खींचने के लिए क्वेरी। यहां, हमने StudentId 102 के साथ StudentDetails को $set का उपयोग करके हटा दिया:

> db.pullAllElementDemo.update( {StudentId:102}, { "$set": { "StudentDetails": [] } } );
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

उपरोक्त संग्रह से सभी दस्तावेज़ प्रदर्शित करें यह जांचने के लिए कि सरणी से वे विशिष्ट एलिमेंट खींचे गए हैं या नहीं:

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

यह निम्नलिखित आउटपुट उत्पन्न करेगा:

{ "_id" : ObjectId("5ccdd9c8685b30d09a7111e4"), "StudentId" : 101, "StudentDetails" : [ { "StudentName" : "Carol", "StudentAge" : 21, "StudentCountryName" : "US" }, { "StudentName" : "Chris", "StudentAge" : 24, "StudentCountryName" : "AUS" } ] }
{ "_id" : ObjectId("5ccdd9f7685b30d09a7111e5"), "StudentId" : 102, "StudentDetails" : [ ] }
  1. - MongoDB में एम्बेडेड ऐरे से विशिष्ट एलिमेंट्स कैसे प्राप्त करें?

    विशिष्ट एलिमेंट्स प्राप्त करने के लिए डॉट नोटेशन के साथ $match का उपयोग करें। नमूना डेटा बनाना (एम्बेडेड ऐरे) db.demo641.insert( { ProductId:101, ProductInformation: [ { ProductName:Product-1, ProductPrice:1000 }, { ProductName:Product-2, ProductPrice:500 }, { ProductName:Product-3

  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. - C# में ऐरे से एलिमेंट्स कैसे एक्सेस करें?

    सबसे पहले, ऐरे को परिभाषित और इनिशियलाइज़ करें:int[] p = new int[3] {99, 92, 95};अब, ऐरे एलिमेंट्स प्रदर्शित करें:for (j = 0; j < 3; j++) { Console.WriteLine(Price of Product[{0}] = {1}, j, p[j]); }किसी भी एलिमेंट को एक्सेस करने के लिए, बस उस एलिमेंट का इंडेक्स शामिल करें जिसे आप चाहते हैं:p[2];