mongodbmongo-cxx-driver

Atomic Operations with MongoDB + MongoCXX


I am new to MongoDB, but experienced with C++. I am using the MongoCXX driver within my application. What I need to do now is to increment a counter shared by multiple programs. My thought was that this requires an atomic operation, but I do not see how to do this with MongoCXX.

Searching, I have found that it appears as though a legacy, deprecated API has exactly what I need:

http://mongocxx.org/api/legacy-v1/atomic__word__intrinsics_8h_source.html

In here, I see exactly what I want to do:

WordType fetchAndAdd(WordType increment)

However, I do not see anything like this in the current C++ driver.

http://mongocxx.org/api/mongocxx-3.6.2/annotated.html

Is there any way to do this type of operation with the current API that I am missing?


Solution

  • You can use something like https://docs.mongodb.com/manual/reference/operator/update/inc/.

    Some drivers provide wrappers for server commands, but such wrappers aren't strictly necessary because you can always invoke the server commands directly.