Reputation: 67
The confusing part is the number in the string field can be with leading zeros but my query param will not contain that
Object 1:
{
"_id" : ObjectId("5c3f6aec29c2e3193315b485"),
"flightCode" : "000541300157840"
}
Object 2:
{
"_id" : ObjectId("5c3f6aec29c2e3193315b485"),
"flightCode" : "00054130015784"
}
If my intent is to find flight code that matches number 54130015784, how will I write my query?
Upvotes: 2
Views: 742
Reputation: 382
If you think that your data would have text flight code so the string can be identified, we can use this.
Regex:
54130015784(?="\n)
Explanation:
Positive Lookahead (?="\n)
Assert that the Regex below matches
" matches the character " literally (case sensitive)
\n matches a line-feed (newline) character (ASCII 10)
Example:
https://regex101.com/r/sF0YfH/3
Let me know if it works. If not give a clear idea what you want.
Upvotes: 1
Reputation: 49985
You need $regex operator with following regular expression:
var code = "541300157840";
var regex = "^0*" + code + "$"
db.col.find({ flightCode: { $regex: new RegExp(regex) } })
where *
means that 0
occurs zero or more times which means that it works both for 000541300157840
and for 541300157840
Upvotes: 3