इसके लिए email-id values display करने के लिए forEach() के साथ print() का उपयोग करें।
Collection बनाना
> db.demo690.insertOne({"UserName":"John","UserEmailId":"John@gmail.com"});
{ "acknowledged" : true, "insertedId" : ObjectId("5ea6db31551299a9f98c939c") }
> db.demo690.insertOne({"UserName":"Bob","UserEmailId":"Bob@gmail.com"});
{ "acknowledged" : true, "insertedId" : ObjectId("5ea6db3c551299a9f98c939d") }
> db.demo690.insertOne({"UserName":"David","UserEmailId":"David@gmail.com"});
{ "acknowledged" : true, "insertedId" : ObjectId("5ea6db47551299a9f98c939e") }
सभी Documents Display करना
> db.demo690.find();
Output:
{ "_id" : ObjectId("5ea6db31551299a9f98c939c"), "UserName" : "John", "UserEmailId" : "John@gmail.com" }
{ "_id" : ObjectId("5ea6db3c551299a9f98c939d"), "UserName" : "Bob", "UserEmailId" : "Bob@gmail.com" }
{ "_id" : ObjectId("5ea6db47551299a9f98c939e"), "UserName" : "David", "UserEmailId" : "David@gmail.com" }
Email-ID Get करके Print करने का Query
> db.demo690.find().forEach(function(document) {
... print(document.UserEmailId);
... });
Output:
John@gmail.com Bob@gmail.com David@gmail.com
व्याख्या: find() cursor return करता है, forEach() प्रत्येक document पर iterate करता है। document.UserEmailId से field value access करके print() से console में display किया। यह MongoDB shell scripting के लिए useful है।