Question

I'm testing BSON as a wire protocol, and I need to be able to change some values on a already created BSONObj... the problem is I can't find a way to do this other than recreating the BSONObj from scratch!

Any tips?

Was it helpful?

Solution

I received a reply from Dwight Merriman, from 10gen:

generaly they are immutable -- the common usage is to serialize/deserialize from a cpp object of your choice. See also $set and $inc but those are server-side.

So... there we have it :)

OTHER TIPS

As it was said there is no way to change existing BSONObj. But there is a workaround to do it without recreating it "from scratch". Suppose you have a BSONObj mybson. And you want to change the field "somefield".

mybson = mybson.removeField("somefield");
mybson = BSONObjBuilder().appendElements(mybson).append("somefield", newvalue).obj();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top