Working with collections

Command summary (discussed in the following):

  • Create a Collection:
      use <dbName>;       db.<collection>.insertOne({ // document });       or       db.createCollection(<collection>);
  • Delete a Collection:
      use <dbName>;       db.<collection>.drop();

The procedure for creating a new collection is identical to the one discussed previously when creating a new database. You first use the database, and then use either of the db.<collection> methods insertOne() or insertMany() to add one or more documents. Alternatively, you can use the db.createCollection() method to create an empty collection. The command show collections shows you which collections have been defined for this database:

To remove the collection, simply issue the command

Get MongoDB 4 Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.