显示未定义和确切的 MongoDB 文档记录
为此,请使用 forEach()。若要显示值,请使用 printjson()。让我们创建一个带有文档的集合 −
> db.demo496.insertOne({"Name":"David","CountryName":"US"});{
"acknowledged" : true,
"insertedId" : ObjectId("5e84b04ab0f3fa88e22790ce")
}
> db.demo496.insertOne({"Name":"John","CountryName":"AUS"});{
"acknowledged" : true,
"insertedId" : ObjectId("5e84b054b0f3fa88e22790cf")
}
> db.demo496.insertOne({"Name":"Robert","CountryName":"UK"});{
"acknowledged" : true,
"insertedId" : ObjectId("5e84b05db0f3fa88e22790d0")
}在 find() 方法的帮助下显示集合中的所有文档 −
> db.demo496.find();
这将产生以下输出 −
{ "_id" : ObjectId("5e84b04ab0f3fa88e22790ce"), "Name" : "David", "CountryName" : "US" }
{ "_id" : ObjectId("5e84b054b0f3fa88e22790cf"), "Name" : "John", "CountryName" : "AUS" }
{ "_id" : ObjectId("5e84b05db0f3fa88e22790d0"), "Name" : "Robert", "CountryName" : "UK" }以下是显示文档中未定义内容的查询 −
> db.demo496.find({}).forEach( (done, notDone) => { printjson(notDone); });这将产生以下输出 −
undefined undefined undefined
以下是使用 .forEach() 显示文档的查询 −
> db.demo496.find({}).forEach( (done, notDone) => { printjson(done); });这将产生以下输出 −
{
"_id" : ObjectId("5e84b04ab0f3fa88e22790ce"),
"Name" : "David",
"CountryName" : "US"
}
{
"_id" : ObjectId("5e84b054b0f3fa88e22790cf"),
"Name" : "John",
"CountryName" : "AUS"
}
{
"_id" : ObjectId("5e84b05db0f3fa88e22790d0"),
"Name" : "Robert",
"CountryName" : "UK"
}
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP