为此,请使用forEach()
和print()
来显示电子邮件ID值。让我们创建一个包含文档的集合-
> 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") }
在find()
方法的帮助下显示集合中的所有文档-
> db.demo690.find();
这将产生以下输出-
{ "_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" }
以下是从MongoDB文档获取电子邮件ID并使用以下命令进行打印的查询print()
-
> db.demo690.find().forEach(function(document) { ... print(document.UserEmailId); ... });
这将产生以下输出-
John@gmail.com Bob@gmail.com David@gmail.com