In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces how to use fuzzy query in mongo, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, let the editor take you to understand it.
The following is a comprehensive application of mongo query, that is, to introduce a practical application of fuzzy query in production, of course, it is also very simple, mainly using fuzzy query and $or query in mongo, as well as the relationship between unions. Here is a record in a mongo.
{"_ id": "ffe6a068-9043-4334-97d2-75387340e655", "file_id": "ffe6a068-9043-4334-97d2-75387340e655", "name": "China Zhengda", "update_time": NumberInt (1554975642), "create_time": NumberInt (1554975642), "content": "China Zhengda related Information", "file_url": "," file_type ":" "user_ids": [1.0,10.0], "group_ids": [], "is_common": NumberInt (0), "confidence":-1.0, "obj_id": "," source ":", "content_time": "," author ":", "summary": "," info_type ":" 00 " "sub_info_type": "," title ":", "word_num": NumberInt (8)}
The requirement for the above record or more records in our production is to query the record in the collection (the set in mongo is the table in mysql), the record that contains the word "positive big" in name or content (the keyword is randomly entered by the user, which is actually a variable), and the value of the timestamp is greater than a certain start time and an end time (this is also selected by the user at the front end. Then we get the request from the front end to query), and the file type, that is, the value of the info_type field is "00". "00" represents word and one of the conditions we get after the front-end user chooses it. Of course, there are other conditions that can be used freely if you want to try.
Here are the above requirements implemented using the mongo statement:
Db.getCollection ("subscribe_test") .find ({$or: [{"name": {"$regex": "Zhengda"}, {"content": {"$regex": "Zhengda"}}], "update_time": {$gte:1,$lte:2000000000}, info_type: "00"})
We sometimes choose to query in the program, and some partners will ask how the above mongo statement is implemented in the programming language. The following is implemented in python language. We will refer to a module in python that operates mongo, that is, the pymongo module, which can be installed with one click in the console or cmd using pip install pymongo. As for how to use it, it is also very simple. You can use Baidu on your own or visit another blog post of mine: the simple use of pymono. The business code that implements the above requirements with python code is attached below:
Import pymongoimport re# create database connection client = pymongo.MongoClient (host='127.0.0.1', port=8014) # enter the ip and port of your local database or the ip and port# of the remote server database to specify the database db1 If not, create database db1db = client.dataretrieve# specify table collection = db.subscribe_test "" 1, query the data in the table "db.collection.find (query, projection) query: optional, use query operator to specify query condition projection: optional, use projection operator to specify the returned key. When querying, return all the key values in the document, just omit the parameter (by default). "" Query = {} query ["$or"] = [{"name": re.compile ("query")}, {"content": re.compile ("Zhengda")},] query ["file_type"] = "00" query ["update_time"] = {"$gte": 0, "$lte": 2000000000} row=collection.find (filter=query) for rin row: print (r ["content"])
The following is the actual development code in production. For reference only, some of the constants above are replaced with the data requested from the front end:
Def person_handler (req_params, page_size Search_offset): "" go to mongo to query personal data: param req_params:: param page_size:: param search_offset:: return: "results = [] query = {} update_time = {} if 'start_time' in req_params and req_params [" start_time "]: start_time = int (req_params [" start_time "]) update_time [' $gte'] = start_time if 'end_time' in Req_params and req_params ['end_time']: end_time = int (req_params ["end_time"]) update_time [' $lte'] = end_time if update_time: query ["update_time"] = update_time if 'file_type' in req_params and req_params [' file_type']: query ["file_type"] = req_params ["file_type"] if 'user_ids' in req_params and req_params [' user_ Ids']: query ['user_ids'] = int (req_params [' user_id']) serch_keywords = req_params ["search_keywords"] query ["$or"] = [{"name": re.compile (serch_keywords)} {"content": re.compile (serch_keywords)},] print (query) result = person_mongodao.search (filter=query) .skip (search_offset) .limit (page_size) count = person_mongodao.search (filter=query) .skip (search_offset) .limit (page_size). Count () for row in result: results.append (row) additions = {"word_segs": req_params ["search_keywords"], "remind": 0} print ("query results", results) return results, additions, count
What if a partner says that I don't use the python language, such as java, how to implement it in code? So if you can write mysql to implement the above requirements, this blogger can recommend that you use Studio 3T, a visualization tool of mongo, to convert mysql statements into mongo statements, python statements, java statements, etc.
The mysql statement is also similar to the mongo statement. There is a console to write the mysql statement, and then make a query to convert the result.
Thank you for reading this article carefully. I hope the article "how to use Fuzzy query in mongo" shared by the editor will be helpful to everyone. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.