Construct query filter using the C API

66 views
Skip to first unread message

pjc

unread,
Jun 23, 2017, 10:27:20 PM6/23/17
to mongodb-user
Hi
I am new to using the C API for mongoldb.

Need to construct a query filter using the C API programmatically as I build the string based on values passed to my program.

The filter using the mongo shell is: db.example.find( {"city": { $gte: "London" }} ) which works fine.

I tried this in the C API:

        bson_t* query = BCON_NEW("filter");
BCON_APPEND(query, "{");
BCON_APPEND(query, "city");
BCON_APPEND(query, "{");
BCON_APPEND(query, "$gte");
BCON_APPEND(query, "London");
BCON_APPEND(query, "}");
BCON_APPEND(query, "}");

However the api calls fail with an assert.


I also tried: 

   bson_t* query = BCON_NEW(BCON_UTF8(" {"city": { $gte: "London" }} )"));


Anybody have any ideas? 

A. Jesse Jiryu Davis

unread,
Jun 30, 2017, 1:55:38 PM6/30/17
to mongodb-user
We have a BCON tutorial here:


for your example, do:

bson_t *query = BCON_NEW ("filter", "{", "city", "{", "$gte", BCON_UTF8 ("London"), "}", "}");

Field names and delimiters like "{" and "}" are simple strings, values like "London" are wrapped in a BCON macro.
Reply all
Reply to author
Forward
0 new messages