How to use MongoDB Create Collection

MongoDB Create Collection is a simple command that allows you to create your own collections in the Database Management System. The MongoDB Command is a great option for anyone looking for a system which creates collections itself and integrates additional ones.

What is MongoDB Create Collection?

The NoSQL database MongoDB works differently to relational solutions, such as the MySQL system. Data records are stored as documents in a collection as opposed to tables. These collections are then combined in a database where they can be retrieved, edited and managed. The system also allows different types of data to be stored in a collection. This is possible because it is a schema-free database.

Normally, it is not necessary to create a collection yourself. The system does this when a database is created with MongoDB Create Database and documents have been added to it. However, you should use the MongoDB Create Collection command if you want to create your own collection.

What is the MongoDB Create Collection syntax?

The MongoDB Create Collection syntax is as follows:

db.createCollection (collection_name, options)

The MongoDB method 'db.createCollection' assigns a task to the system. The 'collection_name' section reserves the name of your collection. This is a string. The last term, 'options', is an optional part of the MongoDB Create Collection command. It is a document type which allows you to specify the size of memory used and indexing within the database, for example.

What are the options for MongoDB Create Collection?

There are four different ways to specify the parameter if you choose to include options. These are detailed below:

  • capped: This option decides whether the collection should be capped. Choosing a capped collection means that you must also specify its maximum total size. The oldest entries will be automatically deleted if the collection exceeds your specified maximum size. Capped is displayed as a Boolean value in MongoDB Create Collection, so it is either true or false.
  • autoIndexId: This option is also given a Boolean value. If this is 'true', an index is automatically created in the '_id' field. However, the default value is always 'false'.
  • size: This value specifies the maximum size of a capped collection. It is only needed if the capped option is true. The value must be expressed with a number and the size is calculated in bytes.
  • max: This option allows you to use Create Collection in MongoDB to specify the maximum number of different documents which a capped collection can contain.

What are some examples of a MongoDB Create Collection?

We’ll show you two examples of newly created collections to give you a better understanding of how Create Collection in MongoDB works in practice. In the first version, we create a new collection called 'Example'. The code looks like this:

use test
switched to db test
db.createCollection ("Example")

It becomes a more detailed if we specify additional options. For example, the code could look like this:

db.createCollection ("example", { capped : true, autoIndexID : true, size : 7213400, max : 10000 })

Is MongoDB Create Collection a good solution for additional collections?

While you might not use the MongoDB Create Collection command as often as MongoDB Create Index or MongoDB Create User, the function can still be very useful for organizational purposes. A collection can be easily removed with the MongoDB Drop Collection command if it is no longer required.

Page top