Following is a piece of code where I am trying to query mongodb using mongocxx driver.
/*Code for finding all that match the filter */
mongocxx::cursor cursor = collection.find(
document{} << "Vehicle_Registration" << vReg
<< "Vehicle_Make" << vMake
<< "Vehicle_Model" << vModel
<< "Vehicle_Owner" << vOwner
<< finalize);
Here
Vehicle_Registration, Vehicle_Make, Vehicle_Model, Vehicle_Owner
are fields of the collection.
The value of
vReg, vMake, vModel , vOwner
are as specified by the user on the screen. If a user specifies only some (not all) of these values, then rest of the values remain NULL. To avoid search on NULL values I try to set them to a regular expression { $regex: /./ } so that the NULL values don't affect the search.
This regular expression works on mongo shell and all fields set to this regular expression get ignored and don't affect the search.
But in the code, to set this regular expression I do :
If (vReg == NULL) { vreg = "{ $regex: /./ }" }
and then pass vReg in the document{} as shown in the code at the top
document{} << "Vehicle_Registration" << vReg
Here vReg gets passed as a string "{ $regex: /./ }"
(with quotes) and not as { $regex: /./ }
(without quotes). As a result it is considered a string and not evaluated as a regular expression in the query and hence there are no search results.
Can somebody please help me know how to pass it as regular expression?
Thank you!