如何在 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+ 瀏覽

開啟你的 職業之路

透過完成課程獲得認證

開始
廣告