python mongoDB 模糊查询(多条件)

from pymongo import MongoClient
import re
class MongoDB(object):
    """"""
    #----------------------------------------------------------------------
    def __init__(self, host, port, database):
        """Constructor"""
        self.host = host
        self.port = port
        self.database = database
        self.conn = MongoClient(self.host, self.port)
        self.coll = self.conn[self.database]
  

Mongo = MongoDB('127.0.0.1', 27017, 'wangzuxian')

#whois_dict = Mongo.coll['dirUrl'].find({'URL':re.compile('sh')})
#whois_dict = Mongo.coll['dirUrl'].find({"URL": {"$regex": "com|sh"}})
#query = Mongo.coll['liuxin_1129'].find({"漏洞名称": {"$regex": "Hadoop|Hive|Spark|HBase|HDFS|Hive|Tez|ambari|sqoop|Zookeeper|MapReduce|Kafka|Redis|Yarn|Oozie|impala|Storm|Flume","$options":"i"}},{"_id":0})
try:
    whois_dict = Mongo.coll['dirUrl'].find({"$or":[{"URL":{"$regex":"com"}},{"URL":{"$regex":"sh"}}]})
except Exception,e:
    print e
print whois_dict
s =  whois_dict.distinct('URL')
for i in s:
    print i

1 第一句查询

image.png

2 第二句查询

image.png

3 第三条查询

image.png
#模糊查询
query = Mongo.coll['dirUrl'].find({"URL": {"$regex": "com"}})
#多个模糊查询
query = Mongo.coll['dirUrl'].find({"URL": {"$regex": "com|sh"}})
#过滤字段
query = Mongo.coll['dirUrl'].find({}, {"_id":0, "key": 1})
#时间范围查询
query = Mongo.coll['dirUrl'].find({"URL": {"$gte": "2017-04-25"}})
query = Mongo.coll['dirUrl'].find({"URL": {"$gte": "2017-04-25", "$lt": "2017-04-26"}})

参考链接

https://docs.mongodb.com/manual/reference/operator/query/or/#op._S_or

你可能感兴趣的:(python mongoDB 模糊查询(多条件))