Update multiple values using MongoCxx


My question seems to be small but I’m pretty new to MongoDB programming.
I have a document with key-value pairs. What is the best approach to update one or more fields using MongoCxx.
Here is my query extracted from one of the example for only field “title”.
auto update_one_result = movies.update_one( make_document(kvp(“title”,“The Adventures of Tom Thumb & Thumbelina”)),
make_document( kvp(“$set”, make_document( kvp(“title”, “MOVIE UPDATE TEST”) ) ) ) );

Question 1) Suppose if I have another field called “duration” and I would like to update this. How to do it in single statement. I know writing another update_one command for “duration” field will work.

Q2) Lets assume my document has 100s of values and I would like to change 90 of it. It would be really tough to update each and every field. So I am planning to have the entire document in BSON and then execute update command. Is it possible to update the collection with the modified BSON in single statement.

Hi @Raja_S1

Please take a look at bulkWrite that allows you to perform multiple write operations together.
Here’s a code sample in C++ driver - https://github.com/mongodb/mongo-cxx-driver/blob/master/examples/mongocxx/bulk_write.cpp

For more details, refer to the Driver Bulk API Spec, which describes bulk write operations for all MongoDB drivers.

Thank you for the response. I will try this approach and reach out to here incase of any problem.