0

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) { ... });
Emonale
  • 513
  • 2
  • 7
  • 22
  • Try `{ $regex : /(?<=name\\":\\")\w+(?=\\)/ }` . Yes, no quotes/apostrophes around regexp – Alex Sveshnikov Mar 18 '21 at 15:16
  • Mongoose returns an empty result, while mongo command line displays "uncaught exception: SyntaxError: invalid regexp group : " – Emonale Mar 18 '21 at 15:21
  • What happens if you try some simple regex, like just `{ $regex: /name/ }` ? Does this produce any result? – Alex Sveshnikov Mar 18 '21 at 15:24
  • Yes that one returns all the documents. – Emonale Mar 18 '21 at 15:25
  • Ok, what about `/name":"/` ? If this works, then try `{ $regex : /(?<=name":")\w+(?=")/ }` – Alex Sveshnikov Mar 18 '21 at 15:26
  • That one as well returns all the documents. – Emonale Mar 18 '21 at 15:27
  • So the information is stored in the format `"name":"BINS"`, no backslashes at all. So you don't need them in the regex. `{ $regex : /(?<=name":")\w+(?=")/ }` should work – Alex Sveshnikov Mar 18 '21 at 15:29
  • That one returns the SyntaxError as before.Every doc is in the form: { "_id" : ObjectId("60522f37ac6f04a757a761ac"), "nodes" : "[{\"name\":\"BINS\",\"oxygenation\":7.8,\"oxygenSaturation\":86,\"temperature\":13.3},{\"name\":\"RW3\",\"oxygenation\":6.6,\"oxygenSaturation\":74,\"temperature\":14.2}]", "salinity" : "17.1", "orp" : "0", "chlorophyll" : "0", "date" : "1615998775000", "__v" : 0 } – Emonale Mar 18 '21 at 15:32
  • `db.my_collection.aggregate([{ $project: { names: { $regexFind: { input: "$nodes", regex: /name":"(\w+)/ } } } } ])` – Alex Sveshnikov Mar 18 '21 at 15:49
  • That returns items such as ```{ "_id" : ObjectId("6052282fac6f049c0ba761a1"), "names" : { "match" : "name\":\"BINS", "idx" : 3, "captures" : [ "BINS" ] } }``` and ```{ "_id" : ObjectId("60522708ac6f0412e5a7619c"), "names" : null }``` – Emonale Mar 18 '21 at 15:54
  • Yes, the information you want is inside "captures" – Alex Sveshnikov Mar 18 '21 at 16:28
  • Thanks. Is it possible to get only the list of distinct values? – Emonale Mar 18 '21 at 16:34
  • Probably this post is helpful: [MongoDB extract data from regex](https://stackoverflow.com/questions/54614401/mongodb-extract-data-from-regex) – Alex Sveshnikov Mar 18 '21 at 16:59

0 Answers0