Search code examples
javamongodbbulkupdate

How to multiupdate using a deque instead of find in MongoDB?


I'm trying to overload a method, allowing it to increment the count of several documents in a one multi update to optimize DB accessing. The current (single update) method looks like:

static synchronized void updateDb(String col, DBObject oldDoc) {
       DBCollection collection = database.getCollection(col);
       BasicDBObject inc = new BasicDBObject().append("$inc", new BasicDBObject().append("count", 1));
       collection.update(new BasicDBObject().append("url",oldDoc.get("url").toString()), inc);
}

Now I have looked into update multi and bulk.find.update() but haven't found a solution yet. Basically I want to hand over an (thread-safe) deque of DBObjects to the new method, and have the DB do most of the rest.

PS: the snag I hit is, that I can't get by with a simple find/query, but instead have a collection (best deque) of urls to query for.

Any advice?


Solution

  • Admittedly I am only proficient with the 3.x driver... If I understand your question correctly, you want to increment count by 1 on each matching "url" field. With 3.x it can be done like this:

        // Input is Collection<DBObject> as variable in
    
        // New way to get the collection
        MongoClient client = new MongoClient();
        MongoCollection<Document> collection = client.getDatabase("mydb").getCollection("myColl");
    
        // Prepare list of update URLs
        List<String> urls = in.stream().map(dbObject -> (String) dbObject.get("url")).collect(Collectors.toList());
    
        // Update many with $in and the same update operation
        collection.updateMany(Filters.in("url", urls), Updates.inc("count", 1));
    

    You can also check the update result returned by updateMany for the number of updated documents.

    Note, that due to mongo limitations, the update list itself may not exceed the size of 16MB - you'll have to split in this case.