MongoDB insert With Examples

Today we will look into MongoDB insert commands. Mongo Client can be used to insert a document into a collection. We can also insert an array of documents, today we will look into various operations related to MongoDB insert.

MongoDB insert

For MongoDB insert example, I am using MongoDB version 3.4.7 and java driver version 3.5.0. However these should work with other versions too. Note that mongo java driver API has totally changed in 3.x version, so if you are using older 2.x version then you will have to modify the code accordingly.

MongoDB insert using Mongo Client

Here is a simple example where I am inserting a single document into Person collection.

Also check that it’s returning WriteResult object containing the number of documents inserted. If the collection doesn’t exists, MongoDB will create it.


MongoDB insert with primary key

Now let’s provide primary key to the mongodb document and see different scenarios that can occur.

MongoDB insert single document with id

What if there is already an object stored with the provided id.

MongoDB insert document duplicate key error

Well, from the output it seems that WriteResult also provide error code and message associated with it, notice that rows inserted is returned as 0.

MongoDB bulk insert

Now let’s try the insert multiple documents with a single command.

So with an array of documents, MongoDB insert operation returns BulkWriteResult that contains more information.

Let’s try to insert array of documents with id also.

Now let’s see what happens when there is an error in inserting one of the documents in the array.

It’s clear that operation is terminated when any of the document insertion fails, any further records are not inserted and we get error message clearly showing which of the record caused the issue.

MongoDB bulk insert ordered and writeConcern parameters

What if we are executing a bulk operation and want to process error documents later on, we can use ordered parameter to tell MongoDB to skip the error document.

Similarly we can also pass writeConcern parameter for different levels of concerns, default write concern is Acknowledged. We will look into it in future post.

MongoDB insert Java Example

Now let’s see different ways to use Java Driver to perform insert operations in MongoDB collection. Note that these java programs are using mongo-java-driver version 3.5.0.

    1. MongoDB insert single document example

A simple program inserting a single document using MongoCollection.insertOne method.

Below image shows the output produced, notice the generated id being printed to console.


We can also pass our own id parameter like below.

However if we are trying to create instance of ObjectId by passing some parameter, such as document.append("_id",new ObjectId("1234"));, it will throw below exception.

It’s because some validations are in place when argument is passed for ObjectId, the passed String should be a hexadecimal string. You can check it’s source code for more details.

If you try to pass ObjectId like document.append("_id", new ObjectId("1234".getBytes()));, it will throw following exception.

The reason is that byte array minimum size should be 12.

    1. MongoDB insert Map java program

If you look at Document example above, you will find it’s very similar to Map. We can insert a Map easily as a document, sample code snippet is given below.

    1. MongoDB insert JSON Document

Sometimes we get JSON response, mostly when we are invoking web services. That’s why MongoDB provides an easy way to insert JSON document in MongoDB collection, as shown in below example.

    1. Inserting multiple documents

We can insert multiple documents too using Mongo java driver, a simple example is shown below.

Output produced will be something like below.

That’s all for MongoDB insert example, we will look into more MongoDB features in coming posts.

By admin

Leave a Reply

%d bloggers like this: