We've already covered how to establish a database in prior topics. We already know that there is no command in MongoDB that allows us to build a database. It generates databases automatically.

There is, however, a command to build a collection. In addition, we will learn how to create a MongoDB collection. We will also cover MongoDB drop collection with examples.

Let's start with Defining, Creating and Dropping a MongoDB collection

MongoDB Create Collection

Tables are mentioned when discussing relational databases. Collections, on the other hand, exist in MongoDB. Collections in MongoDB are created automatically when we refer to them in any command. If it does not already exist, MongoDB will create it for you.

MongoDB Collection Creation Example

     name: ?dipak?

If no collection with the name "codesolutionstuff" exists, the above operation will create one. It will be produced automatically. We can also use the "createCollection()" command to create a collection explicitly. We must adhere to the syntax outlined below.

db.createCollection(name, options)

Before proceeding, it is necessary to understand the distinction between collections and cappedCollection. The collection has no size restrictions, whereas cappedCollection does. We can specify the maximum size and number of documents that can be created.

The following parameters are available in MongoDB Create Collection:

Parameter  typedescription
Name            stringThe new collection's name
OptionsdocumentOptional. Options for configuring a capped collection

The options document comprises the fields shown below.

cappedbooleanOptional. True is used to create a capped collection. If you specify true, you must also specify the size parameter.
autoindexidbooleanOptional. The default setting is false. This option disables the automatic creation of an index on the id field.
 sizenumberOptional. The maximum size for capped collection is specified. When the maximum size limit is reached, MongoDB deletes the older documents.
maxnumberOptional. The maximum number of documents that can be stored in a capped collection. This takes second place to size.
validatordocumentOptional. Allows the user to specify collection validation rules.
Validation levelstringOptional. Determines the validation's rigor. Use the words "off," "strict," and "moderate."

Let's look at a MongoDB create collection example.

>use codesolutionstuff
switched to db codesolutionstuff
{ capped:true, size:1000000, max:2})
{ ?ok? : 1 }

To verify, run the show collections command.

>show collections

This implies that a MongoDB collection has been created.

MongoDB Drop Collection

After learning how to construct a collection in MongoDB, let's look at how to drop a collection in MongoDB. MongoDB Drop Collection is even easier to use than it is to create. To remove a collection, type the following command.


If the collection is successfully dropped from MongoDB, it will return true.
Again, you should first determine whether or not your collection exists. Use the show collections command to accomplish this.

>use codesolutionstuff
Switched to db codesolutionstuff
>show collections

Now, for MongoDB Drop collection, use the following syntax-


You've now successfully dropped the collection.

So that was the MongoDB Create Collection Tutorial. We hope you found our explanation about MongoDB Drop Collection useful.


As a result, we've seen how MongoDB builds collections and how MongoDB deletes collections. Simply keep running these lines of code, and everything will take care of itself. Now we'll go over some more fundamental MongoDB concepts. The data types in MongoDB will be covered in the following article.

Recommended Posts

View All

How to Use MongoDB Limit Records

MongoDB Limit Records, Use MongoDB Limit Records, Limit Documents, Query Limitation, MongoDB Database Management.

Understanding MongoDB Data Modeling

Data modelling, in general, contains numerous components that necessitate active participation from a variety of stakeholders

Botman Chatbot integration in Laravel 9 Tutorial

Botman Chatbot integration in Laravel 8 Tutorial. Laravel 8 Botman Chatbot Tutorial. How to work with Chat bots. Building a booking chatbot using BotM...

CRUD Operations in MongoDB

The conventions of a user interface that allow users to view, search, and alter portions of the database are referred to as CRUD operations MongoDB.

MongoDB Tutorial for Beginners

MongoDB is a growing technology that is grabbing the attention of developers. Data is stored as documents in MongoDB, an open source document-oriented...