[CXX-797] Updating entire document without operators such as set fails at execution with Cannot do an empty bulk write Created: 05/Jan/16  Updated: 05/Jan/16  Resolved: 05/Jan/16

Status: Closed
Project: C++ Driver
Component/s: Implementation
Affects Version/s: 0.2.0
Fix Version/s: None

Type: Bug Priority: Major - P3
Reporter: Amit Ambasta Assignee: Tess Avitabile (Inactive)
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on CDRIVER-1060 Bulk write updates require $set, but ... Closed
Epic Link: C++11 Driver MongoDB 3.2

 Description   

As per mongodb documentation, updating entire document is possible as:

update.js

db.people.update(
   { name: "Andy" },
   {
      name: "Andy",
      rating: 1,
      score: 1
   },
   { upsert: true }
)

One need provide update operators such as $set only when updating sub-document. The same should be achieveable with the C++11 driver as:

update.cxx

    {
        // @begin: cpp-update-top-level-fields
        bsoncxx::builder::stream::document filter_builder, update_builder;
        filter_builder << "name"
                       << "Juni";
        update_builder << "cuisine"
                       << "American (New)";
 
        db["restaurants"].update_one(filter_builder.view(), update_builder.view());
        // @end: cpp-update-top-level-fields
    }

However, executing the above code leads to
terminate called after throwing an instance of

stderr

'mongocxx::v0::bulk_write_exception'
  what():  Cannot do an empty bulk write: mongoc command_invalid_arg error



 Comments   
Comment by Tess Avitabile (Inactive) [ 05/Jan/16 ]

amit.ambasta@delhivery.com, replace_one should be used instead of update for replacing an entire document. This is done to prevent accidentally replacing an entire object. We are closing this ticket, but feel free to re-open if you have further comments or suggestions, and we appreciate your testing the new driver.

Comment by Amit Ambasta [ 05/Jan/16 ]

I have been using replace_one or update operators as needed currently. I just wanted to point out the issue. Not sure if this is the intended behaviour but I am guessing this is due to the fact that all modifer operations internally use the bulk operator to execute appropriate changes.

Comment by Tess Avitabile (Inactive) [ 05/Jan/16 ]

Hi amit.ambasta@delhivery.com. I will look into this issue. In the meantime, note that you can use replace_one instead of update_one to replace an entire document.

Comment by Amit Ambasta [ 05/Jan/16 ]

Mongo command w/o upsert option

update_no_upsert.js

db.books.update(
   { item: "XYZ123" },
   {
     item: "XYZ123",
     stock: 10,
     info: { publisher: "2255", pages: 150 },
     tags: [ "baking", "cooking" ]
   }
)

Generated at Wed Feb 07 22:00:22 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.