MongoDB – Create Collection

Themomsco [CPS] IN

In this tutorial, we will learn how to create a collection in MongoDB.

In MongoDB, data is stored in the form of documents. These documents are stored in the collections like posts, users, comments, logs, etc and the collections are stored in the database.

We have already learned how to create a database in MongoDB. Now, in this tutorial, we will get to know how to create a collection in MongoDB.

Ways of Creating a Collection in MongoDB

You can create a collection in MongoDB in the fly when inserting a document or using the createCollection() method.

Let’s understand both ways to create a collection in MongoDB.

Create a Collection in MongoDB in the Fly

The best thing about MongoDB is when you create a collection, you don’t need to create it before, rather when you insert a document you specify collection name in which document to be inserted on.

And, at the time of inserting the document, the collection is created automatically if there is no collection exist.

Syntax: db.collectionName.insert( { “userName” : “Techy Hunger” } )

Now, let’s understand the above syntax using the code below.

> use techyhungerDB

switched to db techyhungerDB
db.posts.insert({
  websiteName: "Techy Hunger",
  founded: 2015,
  businessEmail: "techyhunger@gmail.com"
  website: "techyhunger.com"
})

In the first code block, we have switched to the database name techyhungerDB. If there is a database exists or not it prints the message switched to db techyhungerDB.

Now, the second code block automatically creates a collection “posts” in the fly and inserts the document (JSON data) in the collection name “posts”. 

and, you will see the response of successful creation in terminal or command prompt.

> WriteResult({ "nInserted" : 1 })

You can also verify if the document in the collection is inserted or not using the following command:

Syntax: db.collectionName.find()

> db.posts.find()

the above command will print:

{ 
 "_id" : ObjectId("87bab8b7589446bdc97a0a32")
 "websiteName": "Techy Hunger", 
 "founded": 2015, 
 "businessEmail": "techyhunger@gmail.com" 
 "website": "techyhunger.com" 
}

Creating Collection Using the createCollection() Method

you can also create a collection using the createCollection() method. This method allows us to create a collection before actually insert a document.

Syntax: db.createCollection(name, options)

the first argument is a collection name and the second argument is optional and can take a value like max number of documents to be inserted on, size, storage engine, etc as an object.

See the example below of createCollection() method without options:

> db.createCollection("users")

The above code snippet will create a collection “users” with an empty value.

Creating Collection in MongoDB with Options

Please see the example shown below:

> db.createCollection("activityLogs", { capped : true, size : 62980800, max : 7000 } )

Let’s see what options do when we pass as second arguments while creating a collection.

  • capped: boolean: It takes a  boolean value true or false. When you set it to true you must specify a size parameter also because the capped value true overrides the old entries when it reaches the specified size value.
  • autoIndexId: boolean: It takes a  boolean value true or false. Specify false to disable the automatic creation of an index on the _id field.
  • size: number: This specifies the maximum size of the collection (capped collection) in bytes. It is ignored in other collections.
  • storageEngine: document: It is available for the WiredTiger storage engine only. It allows users to specify the configuration to the storage engine on a per-collection basis when creating a collection. The syntax is like this { <storage-engine-name>: <options> }Please find all available options in details and how to use them here.

Finally, create a collection in MongoDB tutorial is over, Hope you liked it. Feel free to share your thoughts in the comment box.

 

 

Themomsco [CPS] IN

Leave a Reply