mongo-db
  1. mongo-db-create-collection

Create Collection - (Database and Collection Operations)

In MongoDB, a collection stores related data as document. Before you insert any documents into a database, you need to create a collection to store them. In this page, we will discuss how to create collections in MongoDB using the createCollection() method.

Syntax

You can create a collection in MongoDB using the createCollection() method with the following syntax:

db.createCollection(name, options)

Here is the description of the parameters:

  • name: Required. The name of the collection to be created.

  • options: Optional. A document containing options to a create collection operation. Here are some of the common options:

    • capped: Boolean. (Optional) If true, the collection is capped. (Capped collections are fixed-size collections that support high-throughput insert and read operations.)
    • autoIndexId: Boolean. (Optional) If true, automatically create an index on the _id field.
    • size: Number. (Optional) The size of the collection in bytes. This option is valid only if capped is true.
    • max: Number. (Optional) The maximum number of documents that the collection can contain. This option is valid only if capped is true.

Example

To create a collection named "products", you can run the following command in the MongoDB shell:

db.createCollection("products")

You can also specify additional options while creating a collection. For example, you can create a capped collection with a size of 1 MB and a maximum of 1000 documents using the following command:

db.createCollection("products", { capped: true, size: 1000000, max: 1000 })

Output

When you run the createCollection() method, MongoDB creates a new collection with the specified name and options. You can verify that the collection has been created by running the show collections command in the MongoDB shell.

Explanation

In MongoDB, a collection is created implicitly when inserting documents, but you can also create collections explicitly using the createCollection() method. When you create a collection, you can specify various options, such as whether the collection is capped, the size of the collection, and the maximum number of documents that the collection can contain.

Use

You can use the createCollection() method to create collections in MongoDB. Creating collections explicitly can provide better control over the organization of your data and the performance of your queries.

Important Points

  • You can create a collection in MongoDB using the createCollection() method.
  • A collection is created implicitly when you insert documents into it, but you can also create collections explicitly using the createCollection() method.
  • You can specify various options, such as whether the collection is capped, the size of the collection, and the maximum number of documents that the collection can contain, when creating a collection.

Summary

In this page, we discussed how to create collections in MongoDB using the createCollection() method. We covered the syntax, example, output, explanation, use, and important points of the createCollection() method. By creating collections explicitly using the createCollection() method, you can have better control over the organization of your data and the performance of your queries in MongoDB.

Published on: