下面的javascript代码从定义的Mongo集合中查找所有设备错误代码为210或220 的设备
const devices = await Devices.find({"ErrorCode": { "$in": ["210", "220"] }});
我如何能够检索所有错误代码包含210或220作为子字符串的设备。考虑具有错误代码的设备:;错误:210〃;。我也想取回那个设备。
谢谢!任何帮助都将不胜感激!
样本文件:
{
"DeviceId" : "1234567890",
"Status" : "ONLINE",
"ErrorMessage" : "Error Code: 220 Error Message: Duplicate found",
"Code" : "Error Code: 220",
}
尝试正则表达式,添加3种方法,
1($regex
与$or
:
您可以尝试使用$or
条件的$regex
正则表达式运算符
const devices = await Devices.find({
$or: [
{ Code: { $regex: "210" } },
{ Code: { $regex: "220" } }
]
});
游乐场
2(new RegExp
:
const devices = await Devices.find({
Code: {
$in: [new RegExp("210"), new RegExp("220")]
}
});
3(/
斜线:
const devices = await Devices.find({
Code: {
$in: [/210/, /220/] // without double quotes
}
});