Node.JS rejects BSONRegExp

The code below works in Node.JS except if I include the BSONRegExp in the query then it fails. (BSONRegExp is apparently the preferred syntax for a “contains” text search using Studio 3T query code.)

client.connect(url, function (err, client) {

 var db = client.db("dt3_api");

    var collection = db.collection("LCP Production");
 query = {
        "dt_kind": "Group",
        "dt_name": new BSONRegExp("\\.\\*Kaplan\\.\\*", "i")

    var limit = 3;
    var cursor = collection.find(query).limit(limit);
        function(doc) {
        function(err) {
    // Created with Studio 3T, the IDE for MongoDB -

When I run the query as above, I get this error:

          throw err

TypeError: Cannot read property 'db' of null
    at /Users/richardkaplan/Desktop/studio3talt2.js:17:18
    at connectCallback (/Users/richardkaplan/node_modules/mongodb/lib/mongo_client.js:527:5)
    at /Users/richardkaplan/node_modules/mongodb/lib/mongo_client.js:418:11

I have installed the current MongoDB driver.

I am using the mongodb+srv:// format URI.

If I switch to the older format URI for my connection, then the error disappears but the code simply fails to respond at all.

So I suspect there is some issue with what connection string and what driver I am using that is creating a conflict with the BSONRegExp function but otherwise works with other query types.


Problem solved

The query code was generated by Studio 3T

Their support staff have advised:

Our team had a look at the originally generated query code that did not return any result and was able to identify an issue in the Query Code Generator. The original code stated new BSONRegExp("\.\kaplan\.\", “i”) while the code should actually be
new BSONRegExp(".kaplan.", “i”). If you make this change, the code should then successfully return results. We have logged a ticket to address this bug in one of our next releases.

With this change it does work. [Also it is necessary to use the mongo:// version of the connect string, not the mongo+srv:// version.]

Thank you

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.