Để tạo collection mới, dùng phương thức createCollection().
Cách 1: Cú pháp đơn giản nhất:
db.createCollection("yourNewCollectionName");
Cách 2: Cú pháp với tùy chọn:
db.createCollection("yourNewCollectionName", options);
Tham số options có thể bao gồm:
capped: boolean (true/false)autoIndexId: boolean (deprecated từ MongoDB 3.2/3.4)size: số (kích thước tối đa tính bằng byte)max: số (số tài liệu tối đa)
Kiểm tra phiên bản MongoDB:
> db.version(); 4.0.5
Ví dụ Cách 1:
> db.createCollection("userInformation");
{ "ok" : 1 }
> show collections;
userInformation
Ví dụ Cách 2 (capped collection):
> db.createCollection("bookInformation",{capped:true,size:7000000,max:12000});
{ "ok" : 1 }
> show collections;
bookInformation userInformation