I am using MongoDB 3.4 with the C++ driver (latest version, 3.1.1). I need to query for an item via $regex
that starts with the given prefix
. Currently, I use the following query document:
auto query = bsoncxx::builder::stream::document()
<< "item" << bsoncxx::types::b_regex("^" + prefix, "")
<< bsoncxx::builder::stream::finalize;
However, this fails to work when prefix
contains special characters, like $
. Is there a way of escaping prefix
or rewriting the document creation above so that it works regardless of the used characters in prefix
?