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

MongoDB में एकाधिक एटॉमिक अपडेट कैसे करें?


एकाधिक एटॉमिक अपडेट के लिए update() के साथ $set का उपयोग करें। आइए एक कलेक्शन बनाते हैं:

> db.demo699.insertOne({Name:"Chris Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6e370551299a9f98c93a7") }
> db.demo699.insertOne({Name:"David Miller"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6e37a551299a9f98c93a8") }
> db.demo699.insertOne({Name:"Chris Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6e381551299a9f98c93a9") }
> db.demo699.insertOne({Name:"John Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5ea6e38a551299a9f98c93aa") }

find() से सभी डॉक्यूमेंट्स देखें:

> db.demo699.find();

आउटपुट:

{ "_id" : ObjectId("5ea6e370551299a9f98c93a7"), "Name" : "Chris Brown" }
{ "_id" : ObjectId("5ea6e37a551299a9f98c93a8"), "Name" : "David Miller" }
{ "_id" : ObjectId("5ea6e381551299a9f98c93a9"), "Name" : "Chris Brown" }
{ "_id" : ObjectId("5ea6e38a551299a9f98c93aa"), "Name" : "John Doe" }

MongoDB का उपयोग करके एकाधिक एटॉमिक अपडेट करने के लिए क्वेरी:

> db.demo699.update({Name:"Chris Brown"},{ $set : { Name: "Adam Smith"} }, false, true ); WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })

find() से सभी डॉक्यूमेंट्स देखें:

> db.demo699.find();

आउटपुट:

{ "_id" : ObjectId("5ea6e370551299a9f98c93a7"), "Name" : "Adam Smith" }
{ "_id" : ObjectId("5ea6e37a551299a9f98c93a8"), "Name" : "David Miller" }
{ "_id" : ObjectId("5ea6e381551299a9f98c93a9"), "Name" : "Adam Smith" }
{ "_id" : ObjectId("5ea6e38a551299a9f98c93aa"), "Name" : "John Doe" }

  1. - MongoDB में, क्या $in खोज एकाधिक एकल खोजों से तेज़ है?

    हां, $in का उपयोग करना तेज़ है। आइए एक उदाहरण देखें और दस्तावेज़ों के साथ एक संग्रह बनाएं: > db.demo653.insertOne({subject:MySQL}); { acknowledged : true, insertedId : ObjectId(5ea04b274deddd72997713c0) } > db.demo653.insertOne({subject:MongoDB}); { acknowledged : true, insertedId : ObjectId(5

  1. - MongoDB Aggregate में $redact का उपयोग करना

    $redact documents के contents को documents themselves में stored information के आधार पर restrict करता है। Aggregate में $cond के साथ $redact का उपयोग कर सकते हैं। Collection बनाना > db.demo546.insertOne({Value1:10,Value2:20}); { acknowledged : true, insertedId : ObjectId(5e8e263f9e5f92834d7f05d7

  1. - MongoDB updateOne() और insertOne() का उपयोग

    MongoDB insertOne() एक डॉक्यूमेंट को कलेक्शन में डालता है, जबकि updateOne() क्वेरी फ़िल्टर के आधार पर कलेक्शन में एक डॉक्यूमेंट को अपडेट करता है। आइए डॉक्यूमेंट्स के साथ एक कलेक्शन बनाते हैं: > db.demo735.insertOne({id:1,Name:Chris}); { acknowledged : true, insertedId : ObjectId(5ead51b657bb72a1