How to insert multiple documents at once in MongoDB through Java

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 … Read more

How do I partially update an object in MongoDB so the new object will overlay / merge with the existing one

I solved it with my own function. If you want to update specified field in document you need to address it clearly. Example: { _id : …, some_key: { param1 : “val1”, param2 : “val2”, param3 : “val3” } } If you want to update param2 only, it’s wrong to do: db.collection.update( { _id:…} , … Read more