[CDRIVER-2526] Impove the BCON_APPEND_CTX() function for being called multiple times to add multiple values to same bson array Created: 03/Mar/18  Updated: 05/Feb/24

Status: Backlog
Project: C Driver
Component/s: libbson
Affects Version/s: 1.9.1, 1.9.2, 1.9.3
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: liuzhongchao Assignee: Unassigned
Resolution: Unresolved Votes: 0
Labels: bg-rf, neweng
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

linux


Attachments: File bcon.c    
Issue Links:
Related
is related to CDRIVER-504 Convenient API to create BSON arrays Closed
Epic Link: Improve Developer Experience
Backwards Compatibility: Minor Change

 Description   

While adding elements in a bson array, you should call BCON_APPEND_CTX() , but all elements must be placed in the parameter position at once,just like BCON_APPEND_CTX(query, &ctx, "$each", "[", BCON_INT64((int64_t)num[0]), BCON_INT64((int64_t)num[1]), BCON_INT64((int64_t)num[2]) "]") ; However, the number of array elements is unknown, it's difficult to add all elements in the bson array for just calling BCON_APPEND_CTX() one time;
It's need for me to call Call BCON_APPEND_CTX multiple times to add multiple values to the same bson array.



 Comments   
Comment by Rishabh Bisht [ 05/Feb/24 ]

Hi haveTryTwo, can you also try the new API we added with CDRIVER-5064 if that solves your use case?

https://mongoc.org/libbson/current/bson_array_builder_t.html#bson-array-builder-t 

Comment by liuzhongchao [ 03/Apr/18 ]

"bcon_append_ctx_va could be improved to make this code succeed (If we do this it could also resolve CDRIVER-504.)"
yes, it's right. Hope to solve this someday

Comment by A. Jesse Jiryu Davis [ 03/Mar/18 ]

So, you would like to be able to write code like this?:

   bson_t query = BSON_INITIALIZER;
   bcon_append_ctx_t ctx;
 
   bson_iter_t iter;
   bson_iter_t iter2;
 
   bcon_append_ctx_init (&ctx);
 
   BCON_APPEND_CTX (&query, &ctx, "$each", "["); /* fail here */
   BCON_APPEND_CTX (&query, &ctx, BCON_INT64 (0));
   BCON_APPEND_CTX (&query, &ctx, BCON_INT64 (1));
   BCON_APPEND_CTX (&query, &ctx, BCON_INT64 (2));
   BCON_APPEND_CTX (&query, &ctx, "]");
 
   printf ("%s\n", bson_as_json (&query, NULL));
   bson_iter_init_find (&iter, &query, "a");
   bson_iter_recurse (&iter, &iter2);
 
   while (bson_iter_next (&iter2)) {
      printf ("%s\n", bson_iter_key (&iter2));
   }

Currently this code fails with an assertion error in bcon_append_ctx_va:

      BSON_ASSERT (type != BCON_TYPE_END);

However, bcon_append_ctx_va could be improved to make this code succeed.

(If we do this it could also resolve CDRIVER-504.)

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