以下是一个mongo查询的综合应用,即介绍一个生产中实际应用的模糊查询,当然其实也很简单,主要用到mongo中的模糊查询和$or查询,以及并的关系,下面是一个mongo中的一条记录
1{ 2 "_id" : "ffe6a068-9043-4334-97d2-75387340e655", 3 "file_id" : "ffe6a068-9043-4334-97d2-75387340e655", 4 "name" : "中国正大", 5 "update_time" : NumberInt(1554975642), 6 "create_time" : NumberInt(1554975642), 7 "content" : "中国正大相关信息", 8 "file_url" : "", 9 "file_type" : "", 10 "user_ids" : [ 11 1.0, 12 10.0 13 ], 14 "group_ids" : [ 15 16 ], 17 "is_common" : NumberInt(0), 18 "confidence" : -1.0, 19 "obj_id" : "", 20 "source" : "", 21 "content_time" : "", 22 "author" : "", 23 "summary" : "", 24 "info_type" : "00", 25 "sub_info_type" : "", 26 "title" : "", 27 "word_num" : NumberInt(8) 28}
对上面一条记录或者更多条记录我们生产中的需求是:查询出集合中(mongo中的集合即是mysql中的表),name或content中包含"正大"二字的记录(关键词即是用户随机输入的,其实是一个变量),并且时间戳的值大于某一个开始时间和某一个结束时间(这个也是用户在前端进行选择,然后我们拿到前端的请求来进行查询的),并且文件的类型即info_type字段的值为"00",“00”代表的是word也是前端用户选择后我们获取的条件之一,当然还有其他条件想进行尝试可以自由发挥
下面就是使用mongo语句进行实现的上面的需求:
db.getCollection("subscribe_test").find({$or:[{"name":{"$regex":"正大"}},{"content":{"$regex":"正大"}}],"update_time":{$gte:1,$lte:2000000000},info_type:"00"})
对于查询我们有的时候会选择在程序中进行,有的小伙伴会问上面的mongo语句怎么在编程语言中进行实现,下面是用python语言中进行实现的,我们会引用python中操作mongo的一个模块即pymongo模块可以使用pip install pymongo在控制台或cmd中进行一键安装,至于如何使用也很简单,可以自行百度或者访问我的另一篇博客:pymono的简单使用,下面附上用python代码实现上面需求的业务代码:
1import pymongo 2import re 3# 创建数据库连接 4client = pymongo.MongoClient(host='127.0.0.1', port=8014) #填写自己本机数据库的ip和port或者远程服务器数据库的ip和port 5# 指定数据库db1,没有则创建数据库db1 6db = client.dataretrieve 7#指定数据库中指定的表 8collection=db.subscribe_test 9 10"""1、对表中的数据进行查询""" 11""" 12db.collection.find(query, projection) 13query :可选,使用查询操作符指定查询条件 14projection :可选,使用投影操作符指定返回的键。查询时返回文档中所有键值, 只需省略该参数即可(默认省略)。 15""" 16query = {} 17query["$or"] = [ 18 {"name": re.compile("正大")}, 19 {"content": re.compile("正大")}, 20] 21query["file_type"] = "00" 22query["update_time"] = {"$gte": 0,"$lte": 2000000000} 23row=collection.find(filter=query) 24for r in row: 25 print(r["content"])
下面是生产中实际的开发代码,只供参考,只是把上面的一些常量,换成了从前端请求的数据:
1def person_handler(req_params, page_size, search_offset): 2 """ 3 去mongo中查询个人数据 4 :param req_params: 5 :param page_size: 6 :param search_offset: 7 :return: 8 """ 9 results = [] 10 query = {} 11 update_time = {} 12 if 'start_time' in req_params and req_params["start_time"]: 13 start_time = int(req_params["start_time"]) 14 update_time['$gte'] = start_time 15 if 'end_time' in req_params and req_params['end_time']: 16 end_time = int(req_params["end_time"]) 17 update_time['$lte'] = end_time 18 if update_time: 19 query["update_time"] = update_time 20 if 'file_type' in req_params and req_params['file_type']: 21 query["file_type"] = req_params["file_type"] 22 if 'user_ids' in req_params and req_params['user_ids']: 23 query['user_ids'] = int(req_params['user_id']) 24 serch_keywords = req_params["search_keywords"] 25 26 query["$or"] = [ 27 {"name": re.compile(serch_keywords)}, 28 {"content": re.compile(serch_keywords)}, 29 ] 30 print(query) 31 result = person_mongodao.search(filter=query).skip(search_offset).limit(page_size) 32 count = person_mongodao.search(filter=query).skip(search_offset).limit(page_size).count() 33 for row in result: 34 results.append(row) 35 additions = {"word_segs": req_params["search_keywords"], "remind": 0} 36 print("查询结果", results) 37 return results, additions, count
如果有小伙伴说我用的不是python语言譬如java用代码怎么实现呢?那么如果你会写mysql来实现上面的需求的话本博主可以推荐你使用mongo的一款可视化工具Studio 3T来将mysql语句转换成mongo语句,python语句,java语句等

mysql语句也类似mongo语句有一个控制台可以来进行书写mysql语句,然后进行查询之后将结果进行转换

以上就是关于mongo模糊查询的简单使用,希望对你有所帮助,原创不易,转载请说明出处,谢谢!!!!