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);
cursor.forEach(
function(doc) {
console.log(doc);
},
function(err) {
client.close();
}
);
// Created with Studio 3T, the IDE for MongoDB - https://studio3t.com/
});
When I run the query as above, I get this error:
/Users/richardkaplan/node_modules/mongodb/lib/mongo_client.js:421
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.
Thoughts?