การแสดง Undefined และ Exact Document Records
สำหรับการนี้ ให้ใช้ forEach() เพื่อแสดงค่า ใช้ printjson()
สร้าง Collection พร้อมข้อมูล
> 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") }
แสดงเอกสารทั้งหมด
> db.demo496.find();
ผลลัพธ์:
{ "_id" : ObjectId("5e84b04ab0f3fa88e22790ce"), "Name" : "David", "CountryName" : "US" }
{ "_id" : ObjectId("5e84b054b0f3fa88e22790cf"), "Name" : "John", "CountryName" : "AUS" }
{ "_id" : ObjectId("5e84b05db0f3fa88e22790d0"), "Name" : "Robert", "CountryName" : "UK" }
คิวรีแสดง Undefined (Parameter ที่สองของ Callback)
> db.demo496.find({}).forEach( (done, notDone) => { printjson(notDone); });
ผลลัพธ์:
undefined
undefined
undefined
คิวรีแสดง Exact Documents (Parameter แรกของ Callback)
> 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"
}