Đối với bộ sưu tập MongoDB bắt đầu bằng_, sau đây là cú pháp -
db.createCollection(‘_yourCollectionName’);
Chèn truy vấn bằng cú pháp bên dưới -
db.getCollection('_yourCollectionName').insertOne({"yourFieldName1":"yourValue1","yourFieldName2":yourValue2,............N}); Trước tiên, hãy để chúng tôi tạo một bộ sưu tập với các tài liệu -
> 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")
} Sau đây là truy vấn để hiển thị tất cả các tài liệu từ một bộ sưu tập với sự trợ giúp của phương thức find () -
> db.getCollection('_testUnderscoreCollectionDemo').find().pretty(); Điều này sẽ tạo ra kết quả sau -
{
"_id" : ObjectId("5ccfb4a6140b992277dae0e4"),
"StudentFirstName" : "John",
"StudentAge" : 23
}
{
"_id" : ObjectId("5ccfb4af140b992277dae0e5"),
"StudentFirstName" : "Carol",
"StudentAge" : 21
}