not possible to do bulk insert in Java?

2,002 views
Skip to first unread message

Robbie Cheng

unread,
Sep 13, 2011, 10:15:06 AM9/13/11
to mongodb-user, engin...@fliptop.com
we don't find any easy way to do bulk insert in java. We got BsonList
only supports numberic key when we try to do bulk insert. Then, we
tried to inherit from BsonObject, and stores a map inside that class.
But, in the end, what we get from the result is that it 'still'
generates _id as ObjectId even we define the key ourselves already.
ex:
{ "_id" : ObjectId("4e6f537e414ecb46a329e2d8"), "fbb705d066df59a1" :

our manually configured uuid is somehow ignored.

Thanks,

Richard Kreuter

unread,
Sep 13, 2011, 11:01:28 AM9/13/11
to mongodb-user

Scott Hernandez

unread,
Sep 13, 2011, 11:06:34 AM9/13/11
to mongod...@googlegroups.com
There are various ways to do bulk/batch inserts using the java driver.
Here are some of the the methods:
http://api.mongodb.org/java/current/com/mongodb/DBCollection.html#insert(java.util.List)
http://api.mongodb.org/java/current/com/mongodb/DBCollection.html#insert(com.mongodb.DBObject...)

Here is a sample unit test which does this:
https://github.com/mongodb/mongo-java-driver/blob/master/src/test/com/mongodb/JavaClientTest.java#L634

If you follow the Java Tutorial you will see that you use
(Basic)DBObject instances not the BSONObject class when working with
the database. The BSON stuff is just for low-level encoding/decoding.
http://www.mongodb.org/display/DOCS/Java+Tutorial

> --
> You received this message because you are subscribed to the Google Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.
>
>

Robbie Cheng

unread,
Sep 13, 2011, 9:52:07 PM9/13/11
to mongodb-user
Thanks for your reply.
In fact, we're using save() instead of insert() which only accepts
BasicDBList which is a subclass of BasicBsonList.

here is our code snippet:

BasicDBList persons = new BasicDBList();
for (HashMap<String, Object> person : list){
BasicDBObject object = new BasicDBObject(person);
persons.add(object);
}
coll.save(persons);

exception:
java.lang.IllegalArgumentException: BasicBSONList can only work with
numeric keys, not: [_id]
at org.bson.types.BasicBSONList._getInt(BasicBSONList.java:161)
at org.bson.types.BasicBSONList._getInt(BasicBSONList.java:152)
at org.bson.types.BasicBSONList.get(BasicBSONList.java:104)
at com.mongodb.DBCollection.save(DBCollection.java:625)
at com.mongodb.DBCollection.save(DBCollection.java:608)

Thanks,

On Sep 13, 11:06 pm, Scott Hernandez <scotthernan...@gmail.com> wrote:
> There are various ways to dobulk/batch inserts using thejavadriver.
> Here are some of the the methods:http://api.mongodb.org/java/current/com/mongodb/DBCollection.html#ins...)http://api.mongodb.org/java/current/com/mongodb/DBCollection.html#ins......)
>
> Here is a sample unit test which does this:https://github.com/mongodb/mongo-java-driver/blob/master/src/test/com...
>
> If you follow theJavaTutorial you will see that you use
> (Basic)DBObject instances not the BSONObject class when working with
> the database. The BSON stuff is just for low-level encoding/decoding.http://www.mongodb.org/display/DOCS/Java+Tutorial
>
>
>
>
>
>
>
> On Tue, Sep 13, 2011 at 7:15 AM, Robbie Cheng <robbiech...@gmail.com> wrote:
> > we don't find any easy way to dobulkinsertinjava. We got BsonList
> > only supports numberic key when we try to dobulkinsert. Then, we

Scott Hernandez

unread,
Sep 13, 2011, 10:03:22 PM9/13/11
to mongod...@googlegroups.com

Save doesn't take a list, but only a single document. Save only saves one single dociment at a time.

http://api.mongodb.org/java/current/com/mongodb/DBCollection.html#save(com.mongodb.DBObject)

Reply all
Reply to author
Forward
0 new messages