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

MongoDB में कर्सर इटरेट करके डॉक्यूमेंट कैसे प्रिंट करें?

MongoDB में कर्सर इटरेट करके डॉक्यूमेंट्स प्रिंट करना

MongoDB में find() से लौटे कर्सर को इटरेट करके प्रत्येक डॉक्यूमेंट प्रिंट करने के लिए forEach() के साथ printjson का उपयोग करें।

उदाहरण: कलेक्शन और डॉक्यूमेंट्स बनाना

> db.cursorDemo.insertOne({"StudentFullName":"John Smith","StudentAge":23});
{ "acknowledged" : true, "insertedId" : ObjectId("5cc7f0d08f9e6ff3eb0ce442") }
> db.cursorDemo.insertOne({"StudentFullName":"John Doe","StudentAge":21});
{ "acknowledged" : true, "insertedId" : ObjectId("5cc7f0df8f9e6ff3eb0ce443") }
> db.cursorDemo.insertOne({"StudentFullName":"Carol Taylor","StudentAge":20});
{ "acknowledged" : true, "insertedId" : ObjectId("5cc7f0eb8f9e6ff3eb0ce444") }
> db.cursorDemo.insertOne({"StudentFullName":"Chris Brown","StudentAge":24});
{ "acknowledged" : true, "insertedId" : ObjectId("5cc7f0f88f9e6ff3eb0ce445") }

सभी डॉक्यूमेंट्स देखना

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

आउटपुट:

{ "_id" : ObjectId("5cc7f0d08f9e6ff3eb0ce442"), "StudentFullName" : "John Smith", "StudentAge" : 23 }
{ "_id" : ObjectId("5cc7f0df8f9e6ff3eb0ce443"), "StudentFullName" : "John Doe", "StudentAge" : 21 }
{ "_id" : ObjectId("5cc7f0eb8f9e6ff3eb0ce444"), "StudentFullName" : "Carol Taylor", "StudentAge" : 20 }
{ "_id" : ObjectId("5cc7f0f88f9e6ff3eb0ce445"), "StudentFullName" : "Chris Brown", "StudentAge" : 24 }

कर्सर इटरेट करके सभी डॉक्यूमेंट्स प्रिंट करना

> db.cursorDemo.find().forEach(printjson);

आउटपुट:

{ "_id" : ObjectId("5cc7f0d08f9e6ff3eb0ce442"), "StudentFullName" : "John Smith", "StudentAge" : 23 }
{ "_id" : ObjectId("5cc7f0df8f9e6ff3eb0ce443"), "StudentFullName" : "John Doe", "StudentAge" : 21 }
{ "_id" : ObjectId("5cc7f0eb8f9e6ff3eb0ce444"), "StudentFullName" : "Carol Taylor", "StudentAge" : 20 }
{ "_id" : ObjectId("5cc7f0f88f9e6ff3eb0ce445"), "StudentFullName" : "Chris Brown", "StudentAge" : 24 }

केवल विशिष्ट फील्ड्स प्रिंट करना

> db.cursorDemo.find({}, { "StudentFullName": 1, "StudentAge": 1, "_id": 0 }).forEach(printjson)

आउटपुट:

{ "StudentFullName" : "John Smith", "StudentAge" : 23 }
{ "StudentFullName" : "John Doe", "StudentAge" : 21 }
{ "StudentFullName" : "Carol Taylor", "StudentAge" : 20 }
{ "StudentFullName" : "Chris Brown", "StudentAge" : 24 }

अन्य उपयोगी मेथड्स:

  • print() - कॉम्पैक्ट फॉर्मेट में प्रिंट
  • printjsononeline() - सिंगल लाइन JSON
  • कस्टम फंक्शन: .forEach(function(doc) { print(doc.StudentFullName); })
  • toArray() - सभी डॉक्यूमेंट्स को ऐरे में कन्वर्ट: db.cursorDemo.find().toArray()
  1. - MongoDB एग्रीगेशन: दस्तावेज और ऐरे एलिमेंट्स से औसत प्राप्त करें

    इसके लिए, $avg के साथ $group और aggregate() का उपयोग करें। आइए दस्तावेजों के साथ एक कलेक्शन बनाते हैं: > db.demo598.insertOne({ Information:Student, id:100, details:[ {Name:Chris,Marks:75}, {Name:Bob,Marks:55} ] }); { acknowledged : true, insertedId : ObjectId(5e9

  1. - MongoDB और Python

    MongoDB एक व्यापक रूप से उपयोग किया जाने वाला डॉक्यूमेंट डेटाबेस है जो NoSQL DB का एक रूप भी है। Python कुछ पायथन मॉड्यूल्स के माध्यम से MongoDB के साथ इंटरैक्ट कर सकता है और Mongo DB के अंदर डेटा बना और मैनिपुलेट कर सकता है। इस लेख में हम इसे करना सीखेंगे। लेकिन MongoDB आपके सिस्टम में पहले से उपल

  1. - MongoDB एग्रीगेशन और प्रोजेक्शन

    इसके लिए aggregate() के साथ $project का उपयोग करें। एग्रीगेशन में $project पाइपलाइन में अगले चरण के लिए अनुरोधित फील्ड्स के साथ डॉक्यूमेंट्स को पास करता है। आइए एक कलेक्शन बनाते हैं: > db.demo762.insertOne({ _id : { userId:101, userName:Chris }, countryName : US, details : [ { Name : Robert, Due