[CDRIVER-1525] Flexible opts for bulk operations Created: 07/Sep/16  Updated: 11/Dec/16  Resolved: 12/Sep/16

Status: Closed
Project: C Driver
Component/s: Bulk API, libmongoc
Affects Version/s: None
Fix Version/s: 1.5.0

Type: New Feature Priority: Major - P3
Reporter: A. Jesse Jiryu Davis Assignee: Hannes Magnusson
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by PHPC-734 Support providing collation per opera... Closed
is depended on by PHPC-796 Use flexible opts for BulkWrite updat... Closed
is depended on by CDRIVER-1573 Replace bulk_operation_update_with_op... Closed
Documented
is documented by CDRIVER-1536 Document new bulk_operation methods Closed
Epic Link: Flexible opts
Backwards Compatibility: Fully Compatible

 Description   

Implement these new functions:

bool mongoc_bulk_operation_remove_with_opts      (mongoc_bulk_operation_t       *bulk,
                                                  const bson_t                  *selector,
                                                  const bson_t                  *opts,
                                                  bson_error_t                  *error); /* OUT */
bool mongoc_bulk_operation_remove_one_with_opts  (mongoc_bulk_operation_t       *bulk,
                                                  const bson_t                  *selector,
                                                  const bson_t                  *opts,
                                                  bson_error_t                  *error); /* OUT */
bool mongoc_bulk_operation_replace_one_with_opts (mongoc_bulk_operation_t       *bulk,
                                                  const bson_t                  *selector,
                                                  const bson_t                  *document,
                                                  const bson_t                  *opts,
                                                  bson_error_t                  *error); /* OUT */
bool mongoc_bulk_operation_update_with_opts      (mongoc_bulk_operation_t       *bulk,
                                                  const bson_t                  *selector,
                                                  const bson_t                  *document,
                                                  const bson_t                  *opts,
                                                  bson_error_t                  *error); /* OUT */
bool mongoc_bulk_operation_update_one_with_opts  (mongoc_bulk_operation_t       *bulk,
                                                  const bson_t                  *selector,
                                                  const bson_t                  *document,
                                                  const bson_t                  *opts,
                                                  bson_error_t                  *error); /* OUT */

"opts" can be NULL. I think each function appends "opts" to the operation without any validation. Example for write commands:

   opts = BCON_NEW ("upsert", BCON_BOOL (true));
   query = BCON_NEW ("_id", BCON_INT32 (4));
   doc = BCON_NEW ("$inc", "{", "j", BCON_INT32 (1), "}");
   mongoc_bulk_operation_update_with_opts (bulk, query, doc, opts);

This produces:

{update: "collection", 
 updates: [{
    q: {_id: 4}, 
    u: {$inc: {j: 1}}, 
    upsert: true
}]}

Another example:

   /* "One" normally sorts before "one"; make "one" come first */
   opts = BCON_NEW ("collation", "{",
                    "locale", BCON_UTF8 ("en_US"),
                    "caseFirst", BCON_UTF8 ("lower"),
                    "}");
 
   /* set x=1 on the document with _id "One", which now sorts after "one" */
   update = BCON_NEW ("$set", "{", "x", BCON_INT64 (1), "}");
   selector = BCON_NEW ("_id", "{", "$gt", BCON_UTF8 ("one"), "}");
   mongoc_bulk_operation_update_one_with_opts (bulk,
                                               selector,
                                               update,
                                               opts);

Produces:

{update: "collection", 
 updates: [{
    q: {_id: "one"}, 
    u: {$set: {x: 1}}, 
    collation: {locale: "en_US", caseFirst: "lower"}
}]}

Once this is complete, re-enable the bulk upsert and bulk collation examples by reverting this commit:

https://github.com/mongodb/mongo-c-driver/commit/a497587867fe28dac954d0244ca6fb4bfc5e4a38

(Yes, revert the revert.)



 Comments   
Comment by Githook User [ 11/Dec/16 ]

Author:

{u'username': u'ajdavis', u'name': u'A. Jesse Jiryu Davis', u'email': u'jesse@mongodb.com'}

Message: CDRIVER-1525 bulk collation tests skipped by mistake
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/f96509fc71004d4a6e3b5e0db64e033589344001

Comment by Githook User [ 12/Sep/16 ]

Author:

{u'username': u'bjori', u'name': u'Hannes Magnusson', u'email': u'bjori@php.net'}

Message: CDRIVER-1525: Update examples with error argument
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/fa65f58800ec6528c8b105387b2b9689e5e60710

Comment by Githook User [ 12/Sep/16 ]

Author:

{u'username': u'bjori', u'name': u'Hannes Magnusson', u'email': u'bjori@php.net'}

Message: CDRIVER-1525 Flexible opts for bulk operations
Branch: master
https://github.com/mongodb/mongo-c-driver/commit/2c563a5f7bae2bc3cf30d0ea815b16776451070a

Comment by A. Jesse Jiryu Davis [ 09/Sep/16 ]

If users include "multi" in "opts", let's make that an error. You shouldn't be able to call update_one with "multi" true.

Comment by A. Jesse Jiryu Davis [ 09/Sep/16 ]

I'd really like us to add mongoc_bulk_operation_remove_one_with_opts and mongoc_bulk_operation_update_one_with_opts. The server's "multi" option is a bad API. The main problem is that "update" and "remove" have different defaults ("false" and "true", respectively), and the defaults are bad, and users can't remember the defaults.

Therefore, all our driver API specs have fixed the problems with "multi" by distinguishing between "one" and "many" explicitly. For example, the Bulk API has "updateOne" and ""update". That's what we followed in the C Driver. The CRUD Spec is even more explicit, with "updateOne" and "updateMany". There is no bare "update" method in the CRUD Spec. The mongo shell and all the other drivers have now implemented the CRUD API.

I fear we'll confused our users badly by making them learn how "multi" works with bulk operations. Let's stick to the existing bulk API but add "opts".

Comment by Hannes Magnusson [ 08/Sep/16 ]

I don't think its a good idea to have the _one_with_opts variations.
Since "multi" could have been passed in as part of the opts, it means we have to overwrite it.
And even if it wasn't passed, we have to mutate the opts to add it, or copy the entire bson_t.

Generated at Wed Feb 07 21:12:48 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.