I am using MongoDB in my application and was needed to insert multiple documents inside a MongoDB collection . The version I am using is of 1.6
I saw an example here
http://docs.mongodb.org/manual/core/create/
in the
Bulk Insert Multiple Documents Section
Where the author was passing an array to do this .
When I tried the same , but why it isn't allowing , and please tell me how can I insert multiple documents at once ??
package com;
import java.util.Date;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.MongoClient;
public class App {
public static void main(String[] args) {
try {
MongoClient mongo = new MongoClient("localhost", 27017);
DB db = mongo.getDB("at");
DBCollection collection = db.getCollection("people");
/*
* BasicDBObject document = new BasicDBObject();
* document.put("name", "mkyong"); document.put("age", 30);
* document.put("createdDate", new Date()); table.insert(document);
*/
String[] myStringArray = new String[] { "a", "b", "c" };
collection.insert(myStringArray); // Compilation error at this line saying that "The method insert(DBObject...) in the type DBCollection is not applicable for the arguments (String[])"
} catch (Exception e) {
e.printStackTrace();
}
}
}
Please let me know what is the way so that I can insert multiple documents at once through java .
DBCollection.insert
accepts a parameter of typeDBObject
,List<DBObject>
or an array ofDBObject
s for inserting multiple documents at once. You are passing in a string array.You must manually populate documents(
DBObject
s), insert them to aList<DBObject>
or an array ofDBObject
s and eventuallyinsert
them.The above snippet is essentially the same as the command you would issue in the MongoDB shell:
Your insert record format like in MongoDB that query retire from any source EG.
it is mongodb 3.0
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 theinsert...()
functions.To insert multiple documents we can use
insertMany()
- which takes an array of documents as parameter. When executed, it returns multipleid
s for each document in the array. To drop the collection, usedrop()
command. Sometimes, when doing bulk inserts - we may insert duplicate values. Specifically, if we try to insert duplicate_id
s, we'll get theduplicate key error
:MongoDB stops inserting operation, if it encounters an error, to supress that - we can supply
ordered:false
parameter. Ex:Before 3.0, you can use below code in Java
If you are using mongodb version 3.0 , you can use
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: