mongodb像concat mysql查询 [英] mongodb like concat mysql query

查看:145
本文介绍了mongodb像concat mysql查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mysql中有一个表,其中有一些IP的D和C类,并且我使用此表来测试请求IP是否在数据库中或是否属于C类,例如:

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, '%');

(其中192.168.1.12是我要针对我的表测试的请求IP)

(where 192.168.1.12 is the request IP I am testing against my table)

在我的数据库中,如果我具有C格式的ip'192.168.1',我将得到一个匹配项。

In my Database, if I have the ip '192.168.1' in C format, I will get a match.

I在考虑使用MongoDB并在文档中具有相同的信息,其中每个IP(C或D类格式)都是_id(默认情况下已索引)。

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).

如果正则表达式正好相反,我如何使其与MongoDB一起使用? (如果我事先拥有C类)

How can I get it to work with MongoDB if regex would work just the opposite way? (if I had the C class beforehand)

如果我将每个请求IP拆分为一个C类以使用正则表达式进行检查,例如:

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.

推荐答案

您可以使用$ in运算符获取所有匹配项

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.

有帮助吗?

这篇关于mongodb像concat mysql查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆