I'm tes开发者_高级运维ting 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?
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 :)
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();
精彩评论