Đầu tiên, chúng ta hãy tạo một bộ sưu tập với các chữ cái và số, chẳng hạn như -
7664734-541d-r5i5f-845575e-ghfhjrjr3747_demo368
Truy cập bộ sưu tập trên bằng db.getCollection (). Bây giờ chúng ta hãy tạo một bộ sưu tập với tên đã đề cập ở trên -
> db.createCollection('7664734-541d-r5i5f-845575e-ghfhjrjr3747_demo368');
{ "ok" : 1 }
>db.getCollection('7664734-541d-r5i5f-845575e-ghfhjrjr3747_demo368').insertOne({"FirstName":"Chris"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e57e2152ae06a1609a00aea")
}
>db.getCollection('7664734-541d-r5i5f-845575e-ghfhjrjr3747_demo368').insertOne({"FirstName":"David"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e57e21a2ae06a1609a00aeb")
}
>db.getCollection('7664734-541d-r5i5f-845575e-ghfhjrjr3747_demo368').insertOne({"FirstName":"Bob"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5e57e21e2ae06a1609a00aec")
} 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('7664734-541d-r5i5f-845575e-ghfhjrjr3747_demo368').find(); Điều này sẽ tạo ra kết quả sau -
{ "_id" : ObjectId("5e57e2152ae06a1609a00aea"), "FirstName" : "Chris" }
{ "_id" : ObjectId("5e57e21a2ae06a1609a00aeb"), "FirstName" : "David" }
{ "_id" : ObjectId("5e57e21e2ae06a1609a00aec"), "FirstName" : "Bob" }