How to create a collection correctly in MongoDB to avoid “ReferenceError: Not defined” error?


To create a collection correctly you need to use a MongoDB object in call i.e.

db.createCollection("yourCollectionName");

Let us implement the above syntax in order to create a collection and call it using a MongoDB object −

> use sample;
switched to db sample

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

Display all the collections from the above ‘sample’ database −

> db.getCollectionNames();

This will produce the following output −

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

Updated on: 30-Jul-2019

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements