Change BSON field value (C++)

我是研究僧i 提交于 2019-12-10 16:43:46

问题


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?


回答1:


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 :)




回答2:


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


来源:https://stackoverflow.com/questions/5453645/change-bson-field-value-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!