开发者

MongoDB/C++ - Convert string into BsonElement

I have a MongoDb Database. A process is writing into a collection something like this:

{ "_id" : ObjectId("4d773fdbe916004e36de5c55"), "worker" : [ "172.27.93.231", "172.27.93.232" ], "lock" : [ "172.27.93.230", "172.27.93.232" ] }

After that my C++ process get this document.

Now I want that C++ process to add the IP address to the lock field. The other address should stay there too. I have to get the IP addresses which are in this Field. So I created a vector which will filled with them. Looks like this:

mongo::BSONElement helping = task.getField("lock");
vector<mongo::BSONElement> vectorhelp  = helping.Array();

But how it is possible to write the vector and the new IP address开发者_运维技巧 back in the lock field.


http://www.mongodb.org/pages/viewpage.action?pageId=16646453 seems to be a good place to start. If you look at the BSONObjBuilder API documentation there is an append method that takes a vector as an argument.


You could use BSONObjBuilder to construct BSonObj from string.

BSONObjBuilder b;
b.append("name", "Joe");
b.append("age", 33);
BSONObj p = b.obj();

To convert an array to BsonObj, use vals() function:

bo x;
vector<string> strs;
x.vals(strs);

read more here: http://api.mongodb.org/cplusplus/current/classmongo_1_1_b_s_o_n_obj.html

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜