Để tạo collection đúng cách, phải gọi phương thức trên đối tượng db:
db.createCollection("yourCollectionName");
Ví dụ
> use sample;
switched to db sample
> db.createCollection("employeeInformation");
{ "ok": 1 }
Kiểm tra danh sách collection
> db.getCollectionNames();
Kết quả (có employeeInformation):
[
"arraySizeErrorDemo", "atleastOneMatchDemo", "basicInformationDemo",
"combinedAndOrDemo", "convertSQLQueryDemo", "copyThisCollectionToSampleDatabaseDemo",
"countOrSizeDemo", "distinctOnMultipleFieldsDemo", "documentWithAParticularFieldValueDemo",
"employee", "employeeInformation", "findListOfIdsDemo", "findMimimumElementInArrayDemo",
"findSubstring", "getAllRecordsFromSourceCollectionDemo", "getElementWithMaxIdDemo",
"insertDocumentWithDateDemo", "internalArraySizeDemo", "keepTwoColumnsUniqueDemo",
"largestDocumentDemo", "makingStudentInformationClone", "nestedArrayDemo",
"oppositeAddToSetDemo", "prettyDemo", "projectionAnElementDemo",
"replacingEntireDocumentDemo", "returnOnlyUniqueValuesDemo", "searchInInnerDemo",
"selectItemDemo", "selectWhereInDemo", "sourceCollection", "specificFieldDemo",
"studentInformation", "sumOfValueDemo", "sumTwoFieldsDemo", "truncateDemo",
"updateFieldIfValueIsGreaterDemo", "updateInformation", "updateSubObjectDemo",
"userInformation"
]
Lỗi thường gặp: Gõ createCollection("name") mà quên db. prefix → ReferenceError: createCollection is not defined. Luôn dùng db.createCollection().