0
auto cur = collection.find(make_document(kvp("userId", uid)), findOptions);
        auto collects = bsoncxx::builder::basic::array{};

        for (auto &&doc : cur) {
            doc["time"] = "some new value";  // this line can't execute
            collects.append(doc);
        }
response_success(req, collects);

I want to change the doc time field value, so that return the collects to client. but I found that no interface can call from mongocxx.

who can tell me how to do? Thank you!

acm
  • 12,183
  • 5
  • 39
  • 68
H.He
  • 55
  • 5
  • Unfortunately, at this time, the bsoncxx library does not include a mutation API. However... you can write one using bsoncxx. Basically, build a lazily evaluated tree data structure to represent the document structure, and store requested mutations "off to the side". Then, when you want a serialized form, walk the tree data structure, bulk copying still unexplored regions, and substituting in the mutations from the lookaside list. Not saying it is easy, but that is one approach. – acm Mar 07 '18 at 21:39
  • Sounds good, but I'm completely unfamiliar with a tree data structure. – H.He Mar 08 '18 at 01:36
  • Here is how the mongodb server implements document mutation above an immutable BSON api, a situation analagous to yours: https://github.com/mongodb/mongo/tree/master/src/mongo/bson/mutable. If you don't want to build something like that, you can also just manually copy over the fields you want into a new document builder, ignoring the one you want to change, and then add in the one you want to change. – acm Mar 08 '18 at 15:13

0 Answers0