Changing the value of a BSON field (C ++)

I am testing BSON as a wired protocol, and I need to be able to change some values ​​on the already created BSONObj ... the problem is that I cannot find a way to do this except to recreate BSONObj from Scratch!

Any tips?

+4
source share
2 answers

I got a response from Dwight Merriman, from 10gen:

they are generally immutable - the general use is to serialize / deserialize from cpp an object of your choice. See Also $ set and $ inc, but this is the server side.

So ... there we have it :)

+2
source

As said, there is no way to modify an existing BSONObj. But there is a workaround to do this without re-creating it from scratch. Suppose you have BSONObj mybson. And you want to change the "somefield" field.

mybson = mybson.removeField("somefield"); mybson = BSONObjBuilder().appendElements(mybson).append("somefield", newvalue).obj(); 
+1
source

Source: https://habr.com/ru/post/1345480/


All Articles