How To Query In MongoDB Using One Key In C

- 1 answer

I am attempting to search for a key's value in MongoDB via my .c program. essentially In my collection in one of my documents: I have key:value, i want to be able to return said value by passing in the key. I have seen

 query = bson_new ();
 BSON_APPEND_UTF8 (query, "hello", "world");

 cursor = mongoc_collection_find_with_opts( collection, query, NULL, NULL);

I want to be able to search using only hello and return world.



To query a value in MongoDB, with only one key and pair (ex. Key:Value). Regex can be used.

bson_t *query;
bson_append_regex(query, key, -1/* length of key*/, ".", NULL);
cursor = mongoc_collection_find_with_opts(collection, query, NULL, NULL);

The string "." in regex signifies 'any string', thus if you would need to find json value within another key such as:

{ key:

Changing the Regex to "second_key." would locate desired value.
Note This will return any other values that may lay within, paired with mongoc_cursor_t * you can select the desired value you are looking for.