In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to achieve document query operation in MongoDB, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
Basic operation
The concept of cursor exists in many places, such as Cursor in JDBC in Java, Cursor in ResultSet,Android, and so on, and there is a similar concept in MongoDB. When we call the find method, we can return a cursor like this:
Var cursor = db.sang_collect.find ()
There are both the hasNext () method and the next () method in the cursor, and the combination of these two methods can be used to traverse the results, as follows:
While (cursor.hasNext ()) {print (cursor.next ())}
The next () method can get each document that is queried, as follows:
{"_ id": ObjectId ("59f299579babb96c21ddc9e8"), "x": 0.0, "y": 1000.0} / * 2 * / {"_ id": ObjectId ("59f299579babb96c21ddc9e9"), "x": 1.0, "y": 999.0}
If I only want to get a certain field in the document, I can do the following:
While (cursor.hasNext ()) {print (cursor.next () .y)}
Cursor also implements the iterator interface in JavaScript, so we can also directly call the forEach method to traverse:
Cursor.forEach (function (x) {print (x)})
When we call the find method to obtain cursor, shell will not query the database immediately, but will load it when the data is actually used, which is similar to lazy loading in the database framework. Shell will get the first 100 results or the former 4MB data (the smallest between the two) each time, and then shell will no longer have to connect to the database when we call hasNext and next, directly returning the queried data one by one. After all these 100 or 4MB data have been returned, shell will initiate a request for data from MongoDB again.
Limit
Limit is a method in cursor that limits the number of results returned. For example, I only want to get the first three results of a query as follows:
Var cursor = db.sang_collect.find () .limit (3) skip
Skip is also a method in cursor to indicate the number of records skipped. For example, I want to get records from articles 2 to 5, as follows:
Var cursor = db.sang_collect.find () .skip (2) .limit (4)
Skip the first two pieces (0 and 1) and get the last four pieces of data. The combination of skip and limit is a bit similar to limit in MySQL, which can be used for paging, but this paging method is too inefficient.
Sort
Sort is used to implement sorting functions, such as sorting by x, as follows:
Var cursor = db.sang_collect.find () .sort ({XRAMUL 1})
1 indicates ascending order, and-1 indicates descending order.
After reading the above, do you have any further understanding of how to implement document query operation in MongoDB? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.
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.