mongocxx regex example

193 views
Skip to first unread message

Gerard Suades

unread,
Oct 19, 2017, 5:02:18 AM10/19/17
to mongodb-user
It would be useful to have some examples of a regular expressions queries using b_regex type with prefix, suffix, etc.

I am using mongocxx 3.1.2 and a MongoDB 3.4.9

I am trying to do a regex query with some trailing blanks at the end, which shouldn't return any result. But it does.

std::string prefix = "prefix "; //prefix with trailing space
bsoncxx::builder::basic::document query_regex{};
auto regex = bsoncxx::types::b_regex("^" + prefix, "");
query_regex.append(bsoncxx::builder::basic::kvp("text", regex));
auto regex_doc = connection[db][collection].find_one(query_regex.view());

I just was wondering about the syntax on b_regex to build a proper regular expression. 

Any clue on what I am missing or what could be wrong? Is there any default wildcard embedded on b_regex function that I am not aware of?

Thanks

--Gerard

Derick Rethans

unread,
Oct 20, 2017, 6:07:25 AM10/20/17
to mongodb-user
Hi,

I just tried this out with the following code:

```
#include <iostream>
       
#include <bsoncxx/builder/basic/array.hpp>
#include <bsoncxx/builder/basic/document.hpp>
#include <bsoncxx/builder/basic/kvp.hpp>
#include <bsoncxx/json.hpp>
       
#include <mongocxx/client.hpp>
#include <mongocxx/instance.hpp>
#include <mongocxx/options/find.hpp>
#include <mongocxx/uri.hpp>
       
using bsoncxx::builder::basic::kvp;
using bsoncxx::builder::basic::make_array;
using bsoncxx::builder::basic::make_document;
   
int main(int, char**) {
    mongocxx::instance inst{};
    mongocxx::client conn{mongocxx::uri{}};

    auto db = conn["test"];
    auto col = db["regex"];

    {
        std::string prefix = "prefix "; //prefix with trailing space
        bsoncxx::builder::basic::document query_regex{};
        auto regex = bsoncxx::types::b_regex("^" + prefix, "");
        query_regex.append(bsoncxx::builder::basic::kvp("text", regex));
        auto cursor = col.find(query_regex.view());

        for (auto&& doc : cursor) {
            std::cout << bsoncxx::to_json(doc) << std::endl;
        }
    }

}
```

And the following two documents in the "test" database and "regex" collection:

```
> use test
switched to db test
> db.regex.find();
{ "_id" : ObjectId("59e9c5b3966816952ccdb2b6"), "text" : "prefix" }
{ "_id" : ObjectId("59e9c5ce966816952ccdb2b7"), "text" : "prefix " }
{ "_id" : ObjectId("59e9c886966816952ccdb2b8"), "text" : "prefixfootball" }
{ "_id" : ObjectId("59e9c889966816952ccdb2b9"), "text" : "prefix football" }
>
```

And the output (As expected) is:

```
derick@singlemalt:/tmp $ ./foo
{ "_id" : { "$oid" : "59e9c5ce966816952ccdb2b7" }, "text" : "prefix " }
{ "_id" : { "$oid" : "59e9c889966816952ccdb2b9" }, "text" : "prefix football" }
```

From what I can see, you're not doing anything out of the ordinary and using b_regex as you'd expect. It doesn't add any default wildcards. Of course, that your pattern "^prefix " matches also things like "prefix football" (but not prefixfootball) as you're only anchoring to the start of the string.
Could you share a full code snippet (that compiles) and which data your script returns, what is in the collection, and what you expected?

cheers,
Derick
Reply all
Reply to author
Forward
0 new messages