Alex
Alex

Reputation: 1486

MongoDb grouping insert calls?

I am working on an application which is using MongoDb as backend. I am using Java MongoClient for communication with MongoDb. As MongoClinet is thread safe this instance is used by multiple threads for writing data to mongo. My sample code is

BasicDBObject document = new BasicDBObject();
        try{
            document.append("address", address);
            document.append("name", name);
            document.append("voterId", voterId);
            unProcessedPeopleCollection.insert(document);
        } catch (Exception e){
            logger.error("Error occured while unProcessedPeopleCollection in Mongo");
            logger.error(e);
        }

I don't want mongoclient to insert immediately into database. I want it to group multiple calls into single call like after 100 or 1000 insert calls it will actually push the data in mongo. This will help me to reduce Round Trip Time for every call.

Is there any option for this provided by MongoClient library or how can I handle it at programming level.

Upvotes: 0

Views: 63

Answers (1)

Parvin Gasimzade
Parvin Gasimzade

Reputation: 26012

As far as I know, it is not possible with MongoClient. You can manually group calls by adding them into the collection(set, list, etc.) and try to insert into MongoDB after its size reaches 100 or 1000.

Upvotes: 0

Related Questions