如何在MongoDB中正确创建集合,以避免出现“ ReferenceError:未定义”错误?

要正确创建一个集合,您需要在调用中使用MongoDB对象,即

db.createCollection("yourCollectionName");

让我们实现以上语法,以创建一个集合并使用MongoDB对象调用它-

> use sample;
switched to db sample

> db.createCollection("employeeInformation");
{ "ok" : 1 }

显示来自上面的“样本”数据库的所有集合-

> db.getCollectionNames();

这将产生以下输出-

[
   "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"
]