Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert multiple documents at once in MongoDB through Java

People also ask

How can you randomly insert a batch of documents into MongoDB at once?

Introduction to insertMany() in PyMongo If you're using MongoDB to store data, there will be times when you want to insert multiple documents into a collection at once. It's easy to accomplish this task with Python when you use the collection. insert_many() method.

Which method is used to insert multiple documents in MongoDB?

insertMany() can insert multiple documents into a collection.

What is batch insert in MongoDB?

In MongoDB, the Bulk. insert() method is used to perform insert operations in bulk. Or in other words, the Bulk. insert() method is used to insert multiple documents in one go.


DBCollection.insert accepts a parameter of type DBObject, List<DBObject> or an array of DBObjects for inserting multiple documents at once. You are passing in a string array.

You must manually populate documents(DBObjects), insert them to a List<DBObject> or an array of DBObjects and eventually insert them.

DBObject document1 = new BasicDBObject();
document1.put("name", "Kiran");
document1.put("age", 20);

DBObject document2 = new BasicDBObject();
document2.put("name", "John");

List<DBObject> documents = new ArrayList<>();
documents.add(document1);
documents.add(document2);
collection.insert(documents);

The above snippet is essentially the same as the command you would issue in the MongoDB shell:

db.people.insert( [ {name: "Kiran", age: 20}, {name: "John"} ]);

Before 3.0, you can use below code in Java

DB db = mongoClient.getDB("yourDB");
            DBCollection coll = db.getCollection("yourCollection");
            BulkWriteOperation builder = coll.initializeUnorderedBulkOperation();
            for(DBObject doc :yourList)
            {
                builder.insert(doc);
            }
            BulkWriteResult result = builder.execute();
            return result.isAcknowledged();

If you are using mongodb version 3.0 , you can use

MongoDatabase database = mongoClient.getDatabase("yourDB");
            MongoCollection<Document> collection = database.getCollection("yourCollection");
            collection.insertMany(yourDocumentList);

As of MongoDB 2.6 and 2.12 version of the driver you can also now do a bulk insert operation. In Java you could use the BulkWriteOperation. An example use of this could be:

DBCollection coll = db.getCollection("user");
BulkWriteOperation bulk = coll.initializeUnorderedBulkOperation();
bulk.find(new BasicDBObject("z", 1)).upsert().update(new BasicDBObject("$inc", new BasicDBObject("y", -1)));
bulk.find(new BasicDBObject("z", 1)).upsert().update(new BasicDBObject("$inc", new BasicDBObject("y", -1)));
bulk.execute();

Creating Documents

There're two principal commands for creating documents in MongoDB:

  • insertOne()
  • insertMany()

There're other ways as well such as Update commands. We call these operations, upserts. Upserts occurs when there're no documents that match the selector used to identify documents.

Although MongoDB inserts ID by it's own, We can manually insert custom IDs as well by specifying _id parameter in the insert...() functions.

To insert multiple documents we can use insertMany() - which takes an array of documents as parameter. When executed, it returns multiple ids for each document in the array. To drop the collection, use drop() command. Sometimes, when doing bulk inserts - we may insert duplicate values. Specifically, if we try to insert duplicate _ids, we'll get the duplicate key error:


db.startup.insertMany(
  [
  {_id:"id1", name:"Uber"},
  {_id:"id2", name:"Airbnb"},
  {_id:"id1", name:"Uber"},
  ]
  );

MongoDB duplicate key error

MongoDB stops inserting operation, if it encounters an error, to supress that - we can supply ordered:false parameter. Ex:


db.startup.insertMany(
[
{_id:"id1", name:"Uber"},
{_id:"id2", name:"Airbnb"},
{_id:"id1", name:"Airbnb"},
],
{ordered: false}
);

Your insert record format like in MongoDB that query retire from any source EG.

 {
    "_id" : 1,
    "name" : a
}
{
    "_id" : 2,
    "name" : b,
} 

it is mongodb 3.0

FindIterable<Document> resulutlist = collection.find(query);            
List docList = new ArrayList();
for (Document document : resulutlist) {
    docList.add(document);
}

if(!docList.isEmpty()){
    collectionCube.insertMany(docList);
}