- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
MongoDB - Java
- MongoDB - Java Setup
- MongoDB - Java - Create Collection
- MongoDB - Java - Get Collection
- MongoDB - Java - Insert Document
- MongoDB - Java - Retrieve Documents
- MongoDB - Java - Update Document
- MongoDB - Java - Delete Document
- MongoDB - Java - Drop Collection
- MongoDB - Java - List Collections
MongoDB - PHP
- MongoDB - PHP Setup
- MongoDB - PHP - Create Collection
- MongoDB - PHP - Get Collection
- MongoDB - PHP - Insert Document
- MongoDB - PHP - Retrieve Documents
- MongoDB - PHP - Update Document
- MongoDB - PHP - Delete Document
- MongoDB - PHP - Drop Collection
- MongoDB - PHP - List Collections
MongoDB - Advanced
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
MongoDB - Useful Resources
MongoDB - Create Collection
In this chapter, we will see how to create a collection using MongoDB.
The createCollection() Method
MongoDB db.createCollection(name, options) is used to create collection.
Syntax
Basic syntax of createCollection() command is as follows −
db.createCollection(name, options)
In the command, name is name of collection to be created. Options is a document and is used to specify configuration of collection.
| Parameter | Type | Description |
|---|---|---|
| Name | String | Name of the collection to be created |
| Options | Document | (Optional) Specify options about memory size and indexing |
Options parameter is optional, so you need to specify only the name of the collection. Following is the list of options you can use −
| Field | Type | Description |
|---|---|---|
| capped | Boolean | (Optional) If true, enables a capped collection. Capped collection is a fixed size collection that automatically overwrites its oldest entries when it reaches its maximum size. If you specify true, you need to specify size parameter also. |
| autoIndexId | Boolean | (Optional) If true, automatically create index on _id field.s Default value is false. |
| size | number | (Optional) Specifies a maximum size in bytes for a capped collection. If capped is true, then you need to specify this field also. |
| max | number | (Optional) Specifies the maximum number of documents allowed in the capped collection. |
While inserting the document, MongoDB first checks size field of capped collection, then it checks max field.
Example - Creating Collection
Basic syntax of createCollection() method without options is as follows −
mydb>use test
switched to db test
>db.createCollection("myCollection")
{ ok : 1 }
test>
Example - Checking Collections
You can check the created collection by using the command show collections.
>show collections myCollection
Example - Creating Collection with Options
The following example shows the syntax of createCollection() method with few important options −
test> db.createCollection("mycol", { capped : true, size : 6142800, max : 10000 } )
{ ok : 1 }
>
In MongoDB, you don't need to create collection. MongoDB creates collection automatically, when you insert some document.
test>db.tutorialspoint.insert({"name" : "tutorialspoint"})
{
acknowledged: true,
insertedIds: { '0': ObjectId('6964b41f25c4ea6b911e2624') }
}
test>show collections
mycol
mycollection
tutorialspoint
>