In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Query the list of questions:
As you can see from the data returned by this method, there is an answer_number field, that is, how many answers are currently available to this question
1. Query questions first, and then the number of answers, which is the most common way to think of, the code is as follows:
The advantage of this method is that it is simple and direct, but the disadvantage is that there are too many queries. Assuming that there are 100 questions, 101 queries are required to complete. This results in a significant reduction in web page load data.
two。 Use $lookup to query questions and answers at the same time
The $lookup operator for aggregation operations is covered in Chapter 8. You can query two collections at once using $lookup. Suppose there are 100 questions, and you only need to query once to get all the questions and their corresponding answers at the same time. The code to use the aggregation operation with $lookup is as follows:
Implement the "query answer" function:
After entering the answer list page of a question, you can see a description of the question in addition to the answer. This means that on the answer list page, you need to query not only the answer answer collection, but also the question question collection.
Using the $lookup of the aggregate query can improve the efficiency of the query. The corresponding code is as follows:
Lines 2-8: first use $match to filter out the target question, then query the objectid question corresponding to the target question and the corresponding answer, and store the answer in a list named answer_list.
Line 9: the result returned by the aggregate operation is a legal object, and since the ID (Objectid) of the iterable object is not repeated, there must be only one element here. So convert it to a list and take down the element marked 0.
Lines 10-16: record information about the problem.
Lines 17-23: record the contents of each answer.
Line 24: restore the list of answers in the question information.
Restart the website after modifying the query_answer method. Click any question in the question list page and you can enter the answer list page of that question normally.
Realize the function of "question and answer":
The method in the MongoUtil class for the question is insert_question, and the method in the MongoUtil class is insert_answer. Their codes are as follows:
These two methods are very routine data insertion operations.
In the insert_answer method, the parameter question_id is the string form of the Objectld corresponding to the problem, so you need to first convert the question_id into an Objectld object, and then insert it into the MangoDB
To achieve "like" and "step on" persuasion:
The method in the MongoUtil class for the question "like" or "click" is vote_for_ question, and the method for the answer "like" and "tap" is called vote_for_answer. They all use MongoDB's update_one method.
Use the "$inc" operator to increase and subtract fields:
In the basic part of MangoDB, the usage of update_one is:
Handler.update_one ({"name": "xxx"}, {"$set": {"age": 12}})
It means to query a record whose name field value is xxx, and then update the age field of this record to 12
But in this project, the "like" function needs to increase the field vote_up by 1, and the "tap" function needs to increase the vote down field by l, and multiple visitors may "like" a question at the same time, so both "like" and "click" must be atomic operations. You can't first query the vote_up of the current problem, and then use update_one to set a new value.
In order to augment the fields of an atomic operation, you cannot use the "$set" operator and change it to the "$inc" operator. The inc of this corresponds to the English word increase.
The format is:
Handler. Update_one ({"_ id": Objectid of the question or answer, {"$inc": {"vote_up": 1}})
In fact, self-subtraction is to set the value to a negative number in the dictionary corresponding to the value of "$inc". However, because this project needs to record the number of "click", so "like" and "click" are divided into two fields to save. Therefore, whether it is "like" or "tap" is a self-increasing operation.
two。 Realize "like" and "step on"
Modify the like and tap code to achieve their functions:
It is important to note that the incoming value may be vote_up or vote_down, so using it directly as the Key of the $Inc dictionary can automatically like or step on it.
Restart the website after the modification is completed, and you can see that the "like" and "tap" functions have returned to normal.
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.