我是 MongoDB 的新手,我需要使用 NodeJS Mongoose 将正则表达式应用于 find 方法。 db 包含以下元素:
{
"_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 }
并且我需要进入节点字符串以获取所有“名称”值(在示例 BINS 和 CEST 中)。允许我这样做的正则表达式是
/(?<=name\\\"\:\\")\w+(?=\\)/gm
但是在 mongo 和 Mongoose 中尝试使用它时出现错误。对于 mongo,我试图转义所有特殊字符:
db.my_collection.find( { nodes: { $regex: "\(\?<=\\\"name\\\": \\\"\)\\w\+\(\?=\\\\\)" } } )
但我得到一个空结果。
在 Mongoose 上,我尝试了几种转义,例如以下,但我总是收到无效正则表达式的错误。
db.my_collection.find({ nodes : { $regex : '\(?<=name\\\\\\\"\\:\\\\\"\)\\w+\(?=\\\\\)' } }).
exec(function (err, waterSensorsReadings) { ... });