How to convert string to BSON using MongoDB C ++ driver?

Like toString, is there any way to convert a string to a BSON object? I need to delete a document using the C ++ driver, the remove function expects the request to have a BSON object.

+6
source share
1 answer

Use the fromjson method found here:

http://api.mongodb.org/cplusplus/1.5.4/namespacemongo.html#a4f542be0d0f9bad2d8cb32c3436026c2

BSONObj mongo::fromjson ( const string & str ) Create a BSONObj from a JSON <http://www.json.org> string. 

In addition to the JSON extension extensions described here http://mongodb.onconfluence.com/display/DOCS/Mongo+Extended+JSON , this function accepts certain names without quotes and allows single quotes, if necessary, to be used when specifying field names and string values โ€‹โ€‹instead of double quotes. Remote Unicode JSON sequences (forms) are converted to utf8.

Exceptions: MsgAssertionException if parsing fails. The message contained in this statement includes: a rough indication of when the parsing failed.

+8
source

Source: https://habr.com/ru/post/905500/


All Articles