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
db.codesolutionstuff.insert({ 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 | type | description |
Name | string | The new collection's name |
Options | document | Optional. Options for configuring a capped collection |
The options document comprises the fields shown below.
Field | Type | Description |
capped | boolean | Optional. True is used to create a capped collection. If you specify true, you must also specify the size parameter. |
autoindexid | boolean | Optional. The default setting is false. This option disables the automatic creation of an index on the id field. |
size | number | Optional. The maximum size for capped collection is specified. When the maximum size limit is reached, MongoDB deletes the older documents. |
max | number | Optional. The maximum number of documents that can be stored in a capped collection. This takes second place to size. |
validator | document | Optional. Allows the user to specify collection validation rules. |
Validation level | string | Optional. 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 >db.createCollection(?mongodb?, { capped:true, size:1000000, max:2}) { ?ok? : 1 }
To verify, run the show collections command.
>show collections mongodb
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.
db.collection_name.drop()
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 mongodb
Now, for MongoDB Drop collection, use the following syntax-
>db.mongodb.drop() >true
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.
Conclusion
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.