开发者

Change BSON field value (C++)

开发者 https://www.devze.com 2023-02-20 08:45 出处:网络
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

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消