MongoDB

What is insertOne method in MongoDB

MongoDB is a NoSQL database that performs the fundamental operations that a DBMS(Database Management System) can perform. There are several methods and functions supported by MongoDB that are exercised for the insertion process. The insertOne() belongs to the insertion methods and this method is practiced to insert one document in the MongoDB collection. The insertOne() method also creates a collection, if it does not exist.

MongoDB stores data in a document form and these documents are kept inside a collection. The primary purpose of the insertOne method is to add a document in a MongoDB collection; however, if the collection does not exist then this method also creates the collection as well. Following the importance of this method, our today’s guide is focused on demonstrating the use of the insertOne method in MongoDB:

How insertOne works in MongoDB

The syntax to use insertOne method in MongoDB is mentioned below:

>db.collection-name.insertOne()

The above syntax performs the following additional operations:

Creates a collection: If the specified collection does not exist then it will create the collection as well.

Assigns unique id: In any database, the unique id to data is very important. The insertOne() method allows you to add a unique id to any document while performing insertion and this assignment is performed only when the user forgets to assign that id to a document.

How to use the insertOne method in MongoDB

This section contains the example to use the insertOne method in MongoDB; we have broken down several examples to convey the individual usage of this method.

Example 1: Using insertOne() method to create a collection and insert a document

We are using here a MongoDB database named “store” and it contains the following collections:

>show collections

We want to add a document in the “laptops” collection, and it does not exist in the “store“. The below mentioned command will create the collection and add the document into it:

>db.laptops.insertOne({Make: ["ACER","HP","DELL"], processor: ["intel", "AMD"]})

The command is executed, and you can see that a unique id to that document is also generated.

Example 2: Using insertOne() method to handle unique id

The primary key or the unique ids have a key role in manipulating the data in any database. In MongoDB, there are two ways to set a unique id to any document:

Auto assignment: If the unique id is not assigned by the user, then the insertOne() method assigns it to the document on its own. The command below does not contain any unique id:

>db.smartphones.insertOne({OS: "Android", Qty: 500, Make: ["Samsung", "MI","Sony"]})

You can also verify the unique Id assignment by issuing the below mentioned command:

>db.smartphones.find().pretty()

Note: The collection used in this example is named “smartphones“.

Unique Id defined by the user : Moreover, if the user defines the id while insertion then insertOne will not interfere in assigning the id. For example, the command mentioned below insert the document with a “_id” field as well:

>db.distributions.insertOne({_id: 1, OS: "Linux", available: ["Debian", "Linux Mint", "Ubuntu", "Kubuntu"]})

The output shows that the unique id is “1,” as it was defined in the command. Moreover, you can also verify it by the following command:

 >db.distributions.find().pretty()

Note: The collection name is “distributions” in this example.

Conclusion

The extensive list of Mongo functions has eased the data processing mechanism of databases based on MongoDB. In this post, we have provided a brief guide to using the insertOne() method in MongoDB. The insertOne() belongs to the insertion operation of the database and is one of the sub-methods from the insert() method’s family of MongoDB. After following this guide, a novice MongoDB user will be able to insert a new document in a MongoDB collection. Moreover, with this method, an intermediate user can insert a document and make a collection.

About the author

Adnan Shabbir