以下是我尝试使用 mongocxx 驱动程序查询 mongodb 的一段代码。
/*查找所有匹配过滤器的代码*/
mongocxx::cursor cursor = collection.find(
document{} << "Vehicle_Registration" << vReg
<< "Vehicle_Make" << vMake
<< "Vehicle_Model" << vModel
<< "Vehicle_Owner" << vOwner
<< finalize);
这里
Vehicle_Registration、Vehicle_Make、Vehicle_Model、Vehicle_Owner
是集合的字段。
的价值
vReg、vMake、vModel、vOwner
由用户在屏幕上指定。如果用户只指定了这些值中的一部分(不是全部),则其余值保持为 NULL。为了避免搜索 NULL 值,我尝试将它们设置为正则表达式 { $regex: /./ } 以便 NULL 值不会影响搜索。
此正则表达式适用于 mongo shell,设置为此正则表达式的所有字段都将被忽略并且不影响搜索。
但是在代码中,要设置这个正则表达式,我会这样做:
If (vReg == NULL) { vreg = "{ $regex: /./ }" }
然后在 document{} 中传递 vReg,如顶部代码所示
document{} << "Vehicle_Registration" << vReg
这里 vReg 作为字符串"{ $regex: /./ }"
(带引号)而不是{ $regex: /./ }
(不带引号)传递。因此,它被认为是一个字符串,而不是在查询中作为正则表达式进行评估,因此没有搜索结果。
有人可以帮我知道如何将它作为正则表达式传递吗?
谢谢!