Updates with pipelines seems impossible with libmongoc

I would like to perform an update using an aggregation pipeline with libmongoc, but it appears that libbson precludes this possibility.

Here’s a simple example from the mongo shell:

db.stuff.update(
  {“name”: “thing”},
  [
    {“$set”:
      {“notes”:
        {“$concat”: [“$notes”, “ more notes.”]}
      }
    }
  ]
)

mongoc_collection_update_one() — libmongoc 1.18.0 states the following:
*update: A bson_t containing the update to perform. If updating with a pipeline, a bson_t array.

A bson_t “array”, however, appears to always have a key, and having a key here does not work. I have not been able to determine how to create a bson_t which consists of, for example, [{“$set”:{“notes”:{“$concat”:[“$notes”,“ more notes.”]}}}]

Am I missing something, or is this not possible and the documentation wrong?

I figured out how to do this after reading over bson_array_as_json() — libbson 1.18.0 - simply define a document starting with “$set”, and then append that to a newly initialized bson_t with the “0” between.

Here’s an example:

bson_t document, update;
document = *(BCON_NEW(“$set”, “{“, …));
bson_init(&update)
BSON_APPEND_DOCUMENT(&update, “0”, &document);

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.