[英]Retrieve IP address data from mongodb in pymongo using regular expression or pattern matching
I'm using mongodb to store my data and i used to following python script to execute a query to find count of a collection, 我正在使用mongodb来存储数据,并且我习惯于遵循python脚本来执行查询以查找集合数,
collection_name = "prodresultlistCollection_%s_%s" %(sys.argv[1], sys.argv[2])
my_collection = mydb[collection_name]
parameter = "IP addr"
ip = "10.20.30.40"
count1 = my_collection.count({ '$and': [{parameter:'%s' %(ip)}]})
Here the count1
shows the number of rows that have the given ip
value. 这里
count1
显示具有给定ip
值的行数。 This count1
query only counts the number of rows where ip == IP addr
. 此
count1
查询仅计算ip == IP addr
的行数。 But in the database the IP addr
attribute can have one or more IP in the format below, 但是在数据库中,
IP addr
属性可以具有以下格式的一个或多个IP,
10.20.30.40
10.20.30.40,20.35.45.55
10.20.30.40,20.35.45.55,10.10.10.10
etc...
Consider the IP addr
value in database is 10.20.30.40,20.35.45.55
, then whatever pattern of ip
is given the query should retrieve this row. 考虑数据库中的
IP addr
值为10.20.30.40,20.35.45.55
,则无论给出10.20.30.40,20.35.45.55
ip
模式,查询都应检索该行。
ip = 10
ip = 10.20
ip = 10.20.30
ip = 10.20.30.40
ip = 20
ip = 20.35
ip = 20.35.45
ip = 20.35.45.55
In all the above cases of ip
given to the count1
query, that specific row in database with the IP addr
value of 10.20.30.40,20.35.45.55
should be retrieved. 在上述所有给
count1
查询的ip
情况下,都应检索数据库中IP addr
为10.20.30.40,20.35.45.55
特定行。 I tried to solve the problem using regular expressions as given below but it shows Syntax Error in pymongo and in some cases no rows are retrieved. 我试图使用下面给出的正则表达式解决问题,但是它在pymongo中显示了语法错误,在某些情况下没有检索到任何行。
count1 = my_collection.count({ '$and': [{parameter:/'%s'/ %(ip)}]})
count1 = my_collection.count({ '$and': [{parameter:'/%s/' %(ip)}]})
count1 = my_collection.count({ '$and': [{parameter:/%s/ %(ip)}]})
Then i tried to match the IP pattern using regular expression using the code below: 然后我尝试使用下面的代码使用正则表达式匹配IP模式:
import re
IP = raw_input("Enter the IP: ")
S = IP.split(".")
IP_DB = "10.20.30.40,20.35.45.55"
if len(S)==4:
obj = re.search(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$",IP_DB)
elif len(S)==3:
obj = re.search(r"^\d{1,3}\.\d{1,3}\.\d{1,3}",IP_DB)
elif len(S)==2:
obj = re.search(r"^\d{1,3}\.\d{1,3}",IP_DB)
elif len(S)==1:
obj = re.search(r"^\d{1,3}",IP_DB)
else:
print "Invalid IP!!!"
if obj:
print obj.group()
else:
print "Nothing found!!!"
But the problem here is, it only compares the pattern of the IP and not the values. 但是这里的问题是,它仅比较IP的模式而不是值。 For any value of IP given in the pattern
xx.xx.xx.xx
, this code returns true
for matching/searching result. 对于模式
xx.xx.xx.xx
给定的IP的任何值,此代码为匹配/搜索结果返回true
。 Also the 2nd part of the IP is not considered here. 此外,此处不考虑IP的第二部分。 Is there a better way to solve this problem?
有解决这个问题的更好方法吗? I need to retrieve rows from the mongodb database using
ip
in such a way that any pattern of ip
given matches the IP addr
in the database. 我需要使用
ip
从mongodb数据库中检索行,以使给定的ip
任何模式都与数据库中的IP addr
相匹配。 What kind of syntax or regular expression should be given in the count1
query to achieve this? 为实现此目的,在
count1
查询中应给出哪种语法或正则表达式?
As a regex
pattern, pymongo accepts a regular Python regex object
. 作为
regex
模式,pymongo接受常规Python regex object
。 So you can do the following: 因此,您可以执行以下操作:
import re
regex = re.compile('{}'.format(YOUR_IP_ADDR))
count = my_collection.find({'ip_addr_field': regex}).count()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.