|
| 1 | +--- |
| 2 | +id: mongodb-create-collection |
| 3 | +title: MongoDB - Create Collection |
| 4 | +sidebar_label: Create Collection |
| 5 | +sidebar_position: 8 |
| 6 | +tags: [mongodb, create collection, commands] |
| 7 | +description: Learn how to create a collection in MongoDB using the createCollection() method, along with examples, options, and additional commands. |
| 8 | +--- |
| 9 | + |
| 10 | +# MongoDB - Create Collection |
| 11 | + |
| 12 | +In this chapter, we will see how to create a collection using MongoDB. |
| 13 | + |
| 14 | +## The `createCollection()` Method |
| 15 | + |
| 16 | +MongoDB `db.createCollection(name, options)` is used to create a collection. |
| 17 | + |
| 18 | +### Syntax |
| 19 | + |
| 20 | +The basic syntax of the `createCollection()` command is as follows: |
| 21 | + |
| 22 | +```sql |
| 23 | +db.createCollection(name, options) |
| 24 | +``` |
| 25 | + |
| 26 | +In the command, `name` is the name of the collection to be created. `Options` is a document and is used to specify the configuration of the collection. |
| 27 | + |
| 28 | +### Parameters |
| 29 | + |
| 30 | +| Parameter | Type | Description | |
| 31 | +|-----------|--------|------------------------------------------------------------------| |
| 32 | +| name | String | Name of the collection to be created | |
| 33 | +| options | Document | (Optional) Specify options about memory size and indexing | |
| 34 | + |
| 35 | +The `options` parameter is optional, so you need to specify only the name of the collection. Following is the list of options you can use: |
| 36 | + |
| 37 | +### Options |
| 38 | + |
| 39 | +| Field | Type | Description | |
| 40 | +|---------------|---------|--------------------------------------------------------------------------------------------------------------------| |
| 41 | +| 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 the `size` parameter also. | |
| 42 | +| autoIndexId | Boolean | (Optional) If true, automatically creates an index on the `_id` field. Default value is false. | |
| 43 | +| 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. | |
| 44 | +| max | Number | (Optional) Specifies the maximum number of documents allowed in the capped collection. | |
| 45 | + |
| 46 | +While inserting the document, MongoDB first checks the `size` field of the capped collection, then it checks the `max` field. |
| 47 | + |
| 48 | +### Examples |
| 49 | + |
| 50 | +The basic syntax of the `createCollection()` method without options is as follows: |
| 51 | + |
| 52 | +```bash |
| 53 | +> use test |
| 54 | +switched to db test |
| 55 | +> db.createCollection("mycollection") |
| 56 | +{ "ok" : 1 } |
| 57 | +``` |
| 58 | +
|
| 59 | +You can check the created collection by using the command `show collections`. |
| 60 | +
|
| 61 | +```bash |
| 62 | +> show collections |
| 63 | +mycollection |
| 64 | +system.indexes |
| 65 | +``` |
| 66 | +
|
| 67 | +The following example shows the syntax of the `createCollection()` method with a few important options: |
| 68 | +
|
| 69 | +```bash |
| 70 | +> db.createCollection("mycol", { capped: true, autoIndexId: true, size: 6142800, max: 10000 }) |
| 71 | +{ |
| 72 | + "ok" : 0, |
| 73 | + "errmsg" : "BSON field 'create.autoIndexId' is an unknown field.", |
| 74 | + "code" : 40415, |
| 75 | + "codeName" : "Location40415" |
| 76 | +} |
| 77 | +``` |
| 78 | +
|
| 79 | +In MongoDB, you don't need to create a collection explicitly. MongoDB creates the collection automatically when you insert some document. |
| 80 | +
|
| 81 | +```bash |
| 82 | +> db.tutorialspoint.insert({"name" : "tutorialspoint"}) |
| 83 | +WriteResult({ "nInserted" : 1 }) |
| 84 | +> show collections |
| 85 | +mycol |
| 86 | +mycollection |
| 87 | +system.indexes |
| 88 | +tutorialspoint |
| 89 | +``` |
| 90 | +
|
| 91 | +## Collection Creation Diagram |
| 92 | +
|
| 93 | +```mermaid |
| 94 | +graph TD |
| 95 | + A[Create Collection] --> B[createCollection Method] |
| 96 | + B --> C[Syntax] |
| 97 | + B --> D[Parameters] |
| 98 | + D --> E[name] |
| 99 | + D --> F[options] |
| 100 | + C --> G[Example without options] |
| 101 | + C --> H[Example with options] |
| 102 | + H --> I[Insert Document] |
| 103 | + I --> J[Check Collections] |
| 104 | +``` |
| 105 | +
|
| 106 | +## Command Summary |
| 107 | +
|
| 108 | +| Command | Description | |
| 109 | +|--------------------------------------------|-----------------------------------------------------------------------------| |
| 110 | +| `db.createCollection(name, options)` | Create a collection with the specified name and options | |
| 111 | +| `show collections` | List all collections in the current database | |
| 112 | +| `db.collection.insert(document)` | Insert a document into a collection, creating the collection if it does not exist | |
0 commit comments