MongoDB insert

Filed Under: MongoDB

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.


pankaj:mongodb pankaj$ mongo
MongoDB shell version v3.4.7
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.4.7
Welcome to the MongoDB shell.
For interactive help, type "help".
> use journaldev
switched to db journaldev
> show collections
> db.Persons.insert({name:"Pankaj",country:"India"})
WriteResult({ "nInserted" : 1 })
> show collections
Persons
> db.Persons.find()
{ "_id" : ObjectId("59a3d6bf03ca62436260d57a"), "name" : "Pankaj", "country" : "India" }
> 

mongodb insert, mongo insert

Notice that if we don’t provide primary key i.e _id key, MongoDB automatically generates it and it gets stored as ObjectId object.

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


> db.Persons.insert({_id:123,name:"Pankaj",country:"India"})
WriteResult({ "nInserted" : 1 })
> db.Persons.find()
{ "_id" : ObjectId("53fc3dc9b1e38e3716f79113"), "name" : "Pankaj", "country" : "India" }
{ "_id" : 123, "name" : "Pankaj", "country" : "India" }
> 

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

MongoDB insert document duplicate key error


> db.Persons.insert({_id:123,name:"David",country:"India"})
WriteResult({
	"nInserted" : 0,
	"writeError" : {
		"code" : 11000,
		"errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: journaldev.Persons.$_id_  dup key: { : 123.0 }"
	}
})
> 

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.


> db.Persons.insert([{name:"David"},{name:"Kumar"}])
BulkWriteResult({
	"writeErrors" : [ ],
	"writeConcernErrors" : [ ],
	"nInserted" : 2,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})
> 

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.


> db.Persons.insert([{_id:100,name:"David"},{_id:101,name:"Kumar"}])
BulkWriteResult({
	"writeErrors" : [ ],
	"writeConcernErrors" : [ ],
	"nInserted" : 2,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})
> 

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


> db.Persons.insert(
... [{_id:102,name:"PK"},
... {_id:101,name:"Kumar"},
... {_id:103,name:"PK"}])
BulkWriteResult({
	"writeErrors" : [
		{
			"index" : 1,
			"code" : 11000,
			"errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: journaldev.Persons.$_id_  dup key: { : 101.0 }",
			"op" : {
				"_id" : 101,
				"name" : "Kumar"
			}
		}
	],
	"writeConcernErrors" : [ ],
	"nInserted" : 1,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})
> db.Persons.find()
{ "_id" : ObjectId("53fc3dc9b1e38e3716f79113"), "name" : "Pankaj", "country" : "India" }
{ "_id" : 123, "name" : "Pankaj", "country" : "India" }
{ "_id" : ObjectId("53fc40d1b1e38e3716f79114"), "name" : "David" }
{ "_id" : ObjectId("53fc40d1b1e38e3716f79115"), "name" : "Kumar" }
{ "_id" : 100, "name" : "David" }
{ "_id" : 101, "name" : "Kumar" }
{ "_id" : 102, "name" : "PK" }
> 

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.


> db.Persons.insert( [{_id:102,name:"PK"}, {_id:101,name:"Kumar"}, {_id:103,name:"PK"}] ,{ordered:false})
BulkWriteResult({
	"writeErrors" : [
		{
			"index" : 0,
			"code" : 11000,
			"errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: journaldev.Persons.$_id_  dup key: { : 102.0 }",
			"op" : {
				"_id" : 102,
				"name" : "PK"
			}
		},
		{
			"index" : 1,
			"code" : 11000,
			"errmsg" : "insertDocument :: caused by :: 11000 E11000 duplicate key error index: journaldev.Persons.$_id_  dup key: { : 101.0 }",
			"op" : {
				"_id" : 101,
				"name" : "Kumar"
			}
		}
	],
	"writeConcernErrors" : [ ],
	"nInserted" : 1,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})
> db.Persons.find()
{ "_id" : ObjectId("53fc3dc9b1e38e3716f79113"), "name" : "Pankaj", "country" : "India" }
{ "_id" : 123, "name" : "Pankaj", "country" : "India" }
{ "_id" : ObjectId("53fc40d1b1e38e3716f79114"), "name" : "David" }
{ "_id" : ObjectId("53fc40d1b1e38e3716f79115"), "name" : "Kumar" }
{ "_id" : 100, "name" : "David" }
{ "_id" : 101, "name" : "Kumar" }
{ "_id" : 102, "name" : "PK" }
{ "_id" : 103, "name" : "PK" }
> 

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

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

    
    package com.journaldev.mongodb.main;
    
    import java.net.UnknownHostException;
    
    import org.bson.Document;
    
    import com.mongodb.MongoClient;
    import com.mongodb.client.MongoCollection;
    import com.mongodb.client.MongoDatabase;
    
    public class MongoDBInsertExample {
    
    	public static void main(String[] args) throws UnknownHostException {
    
    		MongoClient mongo = new MongoClient("localhost", 27017);
    		MongoDatabase db = mongo.getDatabase("journaldev");
    
    		MongoCollection<Document> col = db.getCollection("Persons");
    
    		Document document = new Document();
    		document.append("name", "Pankaj");
    		document.append("country", "USA");
    
    		col.insertOne(document);
    
    		System.out.println("ID Generated=" + document.getObjectId("_id").toString());
    		mongo.close();
    	}
    
    }
    

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

    We can also pass our own id parameter like below.

    
    document.append("_id",new ObjectId("123456789012".getBytes())); //min byte array length should be 12
    document.append("_id", new ObjectId(new Date())); // ObjectId constructor also takes Date as argument
    

    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.

    
    Exception in thread "main" java.lang.IllegalArgumentException: invalid hexadecimal representation of an ObjectId: [1234]
    	at org.bson.types.ObjectId.parseHexString(ObjectId.java:549)
    	at org.bson.types.ObjectId.<init>(ObjectId.java:239)
    	at com.journaldev.mongodb.main.MongoDBInsertExample.main(MongoDBInsertExample.java:24)
    

    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.

    
    Exception in thread "main" java.lang.IllegalArgumentException: state should be: buffer.remaining() >=12
    	at org.bson.assertions.Assertions.isTrueArgument(Assertions.java:62)
    	at org.bson.types.ObjectId.<init>(ObjectId.java:272)
    	at org.bson.types.ObjectId.<init>(ObjectId.java:249)
    	at com.journaldev.mongodb.main.MongoDBInsertExample.main(MongoDBInsertExample.java:33)
    

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

  3. MongoDB insert Map java program

  4. 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.

    
    Map<String,Object> dataMap = new HashMap<String,Object>();
    dataMap.put("name", "Pankaj");
    dataMap.put("country", "USA");
    dataMap.put("_id",new ObjectId("123459789012".getBytes()));
    
    Document document = new Document(dataMap);
    		
    col.insertOne(document);
    
  5. MongoDB insert JSON Document

  6. 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.

    
    String json = "{ 'name' : 'Pankaj', 'country' : 'USA' }";
    Document document = Document.parse(json);
    
    col.insertOne(document);
    
  7. Inserting multiple documents

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

    
    package com.journaldev.mongodb.main;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bson.Document;
    
    import com.mongodb.MongoClient;
    import com.mongodb.client.MongoCollection;
    import com.mongodb.client.MongoDatabase;
    
    public class MongoDBInsertMultipleExample {
    
    	public static void main(String[] args) {
    		MongoClient mongo = new MongoClient("localhost", 27017);
    		MongoDatabase db = mongo.getDatabase("journaldev");
    
    		MongoCollection<Document> col = db.getCollection("Persons");
    		
    		List<Document> docs = new ArrayList<>();
    		docs.add(new Document("name", "Pankaj"));
    		docs.add(new Document().append("name", "David"));
    		
    		col.insertMany(docs);
    		
    		for(Document doc : docs) {
    			System.out.println("Name="+doc.getString("name")+", ID="+doc.getObjectId("_id").toString());
    		}
    		mongo.close();
    	}
    
    }
    

    Output produced will be something like below.

    
    Name=Pankaj, ID=59a404c9c5130d39760a01a2
    Name=David, ID=59a404c9c5130d39760a01a3
    

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

Comments

  1. Suraj K says:

    Hello Pankaj,

    How can we do this without using Document and hard coded Id .

    I want to get the Object Id generated by the Mongo in SPring. How Can I do this?\

    Please help on this!

    Thanks

  2. Muhsin says:

    Adding python examples would be very valuable .

  3. Jyotsna says:

    Hi Pankaj,

    Nice example. Could you please demonstrate an example for bulk uploads using BulkWriteOperation of a pojo beans.

  4. Sooraj says:

    Hi, I have a question.

    How do i add another element to an embedded document through java? Say below is my document structure

    Name: ABC
    Details:
    { Age:xx
    Salary:xxx
    }
    Now if i want to add another element, say designation to this document, can you please give a sample java code snipplet to do that?

    Thanks for your time.

  5. Nechama says:

    Hi,
    As it seems, when working with bulks, it’s impossible to get the Ids (ObjectId) of the inserted objects.
    actually, I can see that the Ids were added to the builder itself (to the InsertRequest), but there is no access to the documents.
    any idea? this is really a problem as I need to reuse this Ids for other dependent documents.
    thx,Nechama

    1. papi says:

      In the last snippet of code, try this:
      System.out.println(“ID Generated=” + doc1.get(“_id”).toString());

      i.e.:
      MongoClient mongo = new MongoClient(“localhost”, 27017);
      DB db = mongo.getDB(“journaldev”);

      DBCollection col = db.getCollection(“Persons”);

      DBObject doc1 = BasicDBObjectBuilder.start(“name”, “Pankaj”).get();
      //DBObject doc3 = BasicDBObjectBuilder.start(“name”, “Kumar”).append(“_id”, 1234).get();
      DBObject doc2 = BasicDBObjectBuilder.start(“name”, “Kumar”).get();
      BulkWriteOperation operation = col.initializeOrderedBulkOperation();
      operation.insert(doc1);
      //operation.insert(doc3);
      operation.insert(doc2);
      BulkWriteResult result = operation.execute();
      System.out.println(“Documents inserted=” + result.getInsertedCount()); // prints 2
      System.out.println(“result=” + result);

      System.out.println(“ID Generated=” + doc1.get(“_id”).toString());
      System.out.println(“doc1=” + doc1);

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages