For this, use forEach() along with print() to display the email-id values. Let us create a collection with documents −
> db.demo690.insertOne({"UserName":"John","UserEmailId":"[email protected]"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ea6db31551299a9f98c939c")
}
> db.demo690.insertOne({"UserName":"Bob","UserEmailId":"[email protected]"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ea6db3c551299a9f98c939d")
}
> db.demo690.insertOne({"UserName":"David","UserEmailId":"[email protected]"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ea6db47551299a9f98c939e")
}Display all documents from a collection with the help of find() method −
> db.demo690.find();
This will produce the following output −
{ "_id" : ObjectId("5ea6db31551299a9f98c939c"), "UserName" : "John", "UserEmailId" : "[email protected]" }
{ "_id" : ObjectId("5ea6db3c551299a9f98c939d"), "UserName" : "Bob", "UserEmailId" : "[email protected]" }
{ "_id" : ObjectId("5ea6db47551299a9f98c939e"), "UserName" : "David", "UserEmailId" : "[email protected]" }Following is the query to get email-id from a MongoDB document and print using print() −
> db.demo690.find().forEach(function(document) {
... print(document.UserEmailId);
... });This will produce the following output −
[email protected] [email protected] [email protected]