0

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?

Anders Bernard
  • 541
  • 1
  • 6
  • 19

1 Answers1

1

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.

mtj
  • 3,381
  • 19
  • 30
  • Thx, seems I have to update the driver... and change all the DBObjects to Documents and... my workload is pointed out. :D – Anders Bernard Sep 26 '16 at 10:39
  • Thank you, that definitely did help. Took some time upgrading to the 3.3 driver, and I'm still having some trouble with the connection pool, but thats another q. – Anders Bernard Sep 26 '16 at 12:49