MongoDB

MongoDB Basics

CRUD Operations

Insert

Find

Update

Delete

Query Operators

Comparison Operators

Logical Operators

Element Operators

Create collection in MongoDB

To create a collection in MongoDB, you can use the db.createCollection() method in the MongoDB shell. The syntax for creating a collection is as follows:

db.createCollection("<collection_name>", <options>)

Where <collection_name> is the name of the collection you want to create and <options> are the optional parameters that you can use to configure the collection. Here are some examples of how you can create a collection:

  1. Create a collection without any options:

> db.createCollection("users")

This will create a collection named "users" in the current database.

  1. Create a collection with options:

> db.createCollection("users", {
    capped: true,
    size: 100000,
    max: 100
  })

This will create a capped collection named "users" with a maximum size of 100,000 bytes and a maximum number of documents of 100.

You can also create a collection using the MongoCollection class in a MongoDB driver for your programming language. The specific syntax for doing this will depend on the driver you are using.

Last updated