MongoDB Create Col­lec­tion is a simple command that allows you to create your own col­lec­tions in the Database Man­age­ment System. The MongoDB Command is a great option for anyone looking for a system which creates col­lec­tions itself and in­teg­rates ad­di­tion­al ones.

What is MongoDB Create Col­lec­tion?

The NoSQL database MongoDB works dif­fer­ently to re­la­tion­al solutions, such as the MySQL system. Data records are stored as documents in a col­lec­tion as opposed to tables. These col­lec­tions 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 col­lec­tion. This is possible because it is a schema-free database.

Normally, it is not necessary to create a col­lec­tion 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 Col­lec­tion command if you want to create your own col­lec­tion.

What is the MongoDB Create Col­lec­tion syntax?

The MongoDB Create Col­lec­tion syntax is as follows:

db.createCollection (collection_name, options)

The MongoDB method 'db.cre­ate­Col­lec­tion' assigns a task to the system. The 'col­lec­tion_name' section reserves the name of your col­lec­tion. This is a string. The last term, 'options', is an optional part of the MongoDB Create Col­lec­tion 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 Col­lec­tion?

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 col­lec­tion should be capped. Choosing a capped col­lec­tion means that you must also specify its maximum total size. The oldest entries will be auto­mat­ic­ally deleted if the col­lec­tion exceeds your specified maximum size. Capped is displayed as a Boolean value in MongoDB Create Col­lec­tion, so it is either true or false.
  • autoIn­dex­Id: This option is also given a Boolean value. If this is 'true', an index is auto­mat­ic­ally created in the '_id' field. However, the default value is always 'false'.
  • size: This value specifies the maximum size of a capped col­lec­tion. It is only needed if the capped option is true. The value must be expressed with a number and the size is cal­cu­lated in bytes.
  • max: This option allows you to use Create Col­lec­tion in MongoDB to specify the maximum number of different documents which a capped col­lec­tion can contain.

What are some examples of a MongoDB Create Col­lec­tion?

We’ll show you two examples of newly created col­lec­tions to give you a better un­der­stand­ing of how Create Col­lec­tion in MongoDB works in practice. In the first version, we create a new col­lec­tion called 'Example'. The code looks like this:

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

It becomes a more detailed if we specify ad­di­tion­al options. For example, the code could look like this:

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

Is MongoDB Create Col­lec­tion a good solution for ad­di­tion­al col­lec­tions?

While you might not use the MongoDB Create Col­lec­tion command as often as MongoDB Create Index or MongoDB Create User, the function can still be very useful for or­gan­iz­a­tion­al purposes. A col­lec­tion can be easily removed with the MongoDB Drop Col­lec­tion command if it is no longer required.

Go to Main Menu