MongoDB Create Collections – Delete Collections Tutorial from Coding Compiler. In this Create Collection in MongoDB tutorial, we will discuss how to create a collection and how to delete collections in MongoDB. Let’s start learning MongoDB.
MongoDB Create Collections Tutorial
In this MongoDB tutorial, we show you how to use MongoDB to create collections.
What is a MongoDB collection?
What is capped collection in MongoDB?
How To Create Collection In MongoDB
The collection is created in MongoDB using the createCollection() method.
Syntax format:
db.createCollection(name, options)
Parameter Description:
- Name: the name of the collection to create
- Options: optional parameters, specify options for memory size and index
Options can be as follows:
Field | Types of | description |
---|---|---|
capped | Boolean | (Optional) If true, creates a fixed collection. A fixed collection is a collection of fixed sizes that automatically overwrites the oldest document when it reaches its maximum value. When the value is true, the size parameter must be specified. |
autoIndexId | Boolean | (Optional) If true, an index is automatically created in the _id field. The default is false. |
size | Numerical value | (Optional) Specify a maximum value (in bytes) for the fixed collection. This field also needs to be specified if capped is true. |
max | Numerical value | (Optional) Specifies the maximum number of documents that are included in the fixed collection. |
When you insert a document, MongoDB first checks the size field of the fixed collection and then checks the max field.
Instance
Create a runoob collection in the test database:
> use test switched to db test > db.createCollection("runoob") { "ok" : 1 } >
If you want to see an existing collection, you can use the show collections command:
> show collections runoob system.indexes
Here’s how to use createCollection() with a few key parameters:
Create a fixed collection mycol, the entire collection space size is 6142800 KB, and the maximum number of documents is 10000.
> db.createCollection("mycol", { capped : true, autoIndexId : true, size : 6142800, max : 10000 } ) { "ok" : 1 } >
In MongoDB, you don’t need to create a collection. When you insert some documents, MongoDB automatically creates the collection.
> db.mycol2.insert({"name" : "Rookie Tutorial"}) > show collections mycol2 ...
MongoDB Delete Collection Tutorial
Here in this section we show you how to use MongoDB to delete collections.
The drop() method is used in MongoDB to remove collections.
Syntax format:
db.collection.drop()
Parameter Description:
- no
return value
The drop() method returns true if the selected collection was successfully deleted, otherwise it returns false.
Instance
In the database mydb, we can first view the existing collections by using the show collections command:
>use mydb switched to db mydb >show collections mycol mycol2 system.indexes runoob >
Then delete the collection mycol2 :
>db.mycol2.drop() true >
View the collections in the database mydb again with show collections:
>show collections mycol system.indexes runoob >
It can be seen from the results that the mycol2 collection has been deleted.
Related MongoDB Tutorials For Beginners
What is MongoDB Tutorial For Beginners
MongoDB Data Modelling Tutorial
MongoDB Create Database Tutorial