问题
I have a table in Mysql where I have some D and C classes of IPs and I use this table to test if a request IP is in the database OR belongs to the C class like that:
select * from My_Table
where '192.168.1.12' LIKE CONCAT(ip, '%');
(where 192.168.1.12 is the request IP I am testing against my table)
In my Database, if I have the ip '192.168.1' in C format, I will get a match.
I was thinking about using MongoDB and have the same information in documents where each IP (either in C or D class format) is the _id (which is indexed by default).
How can I get it to work with MongoDB if regex would work just the opposite way? (if I had the C class beforehand)
If I split every request IP into a C class to check with regex like:
db.my_table.find(_id: '^/split_ip/')
I would have to check in my application again every result by iteration and I think that wouldn't be a good solution.
So, I am wondering if you have any advice or suggestion to give me.
回答1:
You could use the $in operator to get all matching from a single query.
{ _id: {$in : [/^192.0.1.1/, /^194.1.1/ ] }}
Becuase you're using /^
option in regex, the index will be used.
Does that help?
来源:https://stackoverflow.com/questions/14562076/mongodb-like-concat-mysql-query