I am new to MongoDB and I need to apply a regex to the find method using NodeJS Mongoose. The db contains elements such as:
{
"_id" : ObjectId("1234567899788675432454677"),
"nodes" : "[{\"name\":\"BINS\",\"oxygenation\":7.8,\"oxygenSaturation\":86,\"temperature\":13.3},{\"name\":\"CEST\",\"oxygenation\":4.6,\"oxygenSaturation\":52,\"temperature\":14.7},
"orp" : "0",
"chlorophyll" : "0",
"date" : "1615995776000",
"__v" : 0 }
and I need to go inside the nodes string to get all "name" values (in the example BINS and CEST). The regex that allows me to do it is
/(?<=name\\\"\:\\")\w+(?=\\)/gm
but I get errors when trying to use it both in mongo and Mongoose. For mongo I tried to escape all special characters:
db.my_collection.find( { nodes: { $regex: "\(\?<=\\\"name\\\": \\\"\)\\w\+\(\?=\\\\\)" } } )
but I get an empty result.
On Mongoose I tried several escaping such as the following, but I always get an error of invalid regex.
db.my_collection.find({ nodes : { $regex : '\(?<=name\\\\\\\"\\:\\\\\"\)\\w+\(?=\\\\\)' } }).
exec(function (err, waterSensorsReadings) { ... });