Sử dụng forEach() duyệt cursor kết quả find() và print() để hiển thị giá trị trường UserEmailId.
Tạo 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") }
Hiển thị tất cả:
> db.demo690.find();
Duyệt và in email:
> db.demo690.find().forEach(function(document) {
... print(document.UserEmailId);
... });
Kết quả in ra console:
John@gmail.com Bob@gmail.com David@gmail.com