如何在 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"
]

更新于: 30-Jul-2019

1K+ 浏览

开启你的 职业之路

通过完成课程获得认证

开始
广告
© . All rights reserved.