I have a few mongodb docs like below:
{
...
ipAddress: "1.2.3.4"
},
{
...
ipAddress: "1.2.3.44,1.2.5.7"
}
The ipAddress is a string field that can contain many IPs that are separated by comma. ("1.2.3.4,1.3.5.7,2.4.6.8" etc)
I would like to retrieve a mongo document that matches a particular IP. For which, I try the below query:
collection.findOne({
ipAddress: new RegExp (<<the_ip>>)
});
However, while debugging my app, I realized that the above query matches and finds anything that has the_ip and not just the exact the_ip
Eg. If I have two documents that have IPs as 1.2.2.2 and 1.2.2.22 and I search for regexp 1.2.2.2, I've noticed that it always matches and returns 1.2.2.22. I feel this is expected since technically the regexp is doing it's job.
However this is not the desired outcome. How do I do a full exact IP match?
Thanks.
(^|,)<<the_ip>>(,|$)many IPs that are separated by comma- a better option would be to use an array and$elemMatchto find a specific IP.