สำหรับคอลเลกชัน MongoDB ที่ขึ้นต้นด้วย _ ต่อไปนี้เป็นรูปแบบไวยากรณ์ -
db.createCollection(‘_yourCollectionName’);
แทรกข้อความค้นหาโดยใช้ไวยากรณ์ด้านล่าง -
db.getCollection('_yourCollectionName').insertOne({"yourFieldName1":"yourValue1","yourFieldName2":yourValue2,............N}); ให้เราสร้างคอลเลกชันที่มีเอกสารก่อน -
> db.createCollection('_testUnderscoreCollectionDemo');
{ "ok" : 1 }
>db.getCollection('_testUnderscoreCollectionDemo').insertOne({"StudentFirstName":"John","StudentAge":23});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ccfb4a6140b992277dae0e4")
}
>db.getCollection('_testUnderscoreCollectionDemo').insertOne({"StudentFirstName":"Carol","StudentAge":21});
{
"acknowledged" : true,
"insertedId" : ObjectId("5ccfb4af140b992277dae0e5")
} ต่อไปนี้เป็นแบบสอบถามเพื่อแสดงเอกสารทั้งหมดจากคอลเลกชันโดยใช้วิธี find() -
> db.getCollection('_testUnderscoreCollectionDemo').find().pretty(); สิ่งนี้จะสร้างผลลัพธ์ต่อไปนี้ -
{
"_id" : ObjectId("5ccfb4a6140b992277dae0e4"),
"StudentFirstName" : "John",
"StudentAge" : 23
}
{
"_id" : ObjectId("5ccfb4af140b992277dae0e5"),
"StudentFirstName" : "Carol",
"StudentAge" : 21
}