顯示未定義和確切的 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