Lesson 5 MongoDB Data Query (II): mongodb Data Query
1. Course outlineThis course continues to explain MongoDB Data Query related content, including MongoDB cursor, fuzzy query, and use of findAndModify functions. 1. MongoDB cursor 2. fuzzy query 3. Use of the findAndModify Function
2. the course introduction will first explain the process of returning MongoDB query results, review the content described in the last lesson when I introduced the find function, as mentioned in the last lesson when I introduced the document to query the function find, the returned value is a cursor object. In the actual JavaScript script, the cursor variable is used to save the results returned by the find function, and printjson is used to print all the documents in the cursor variable. In fact, when we use the find function to query documents, MongoDB does not return all documents in the result set at one time, but returns the query results in batches in the form of multiple documents, the returned documents are cached in the memory, and the amount of data returned each time does not exceed 4 MB. The Return Value of the find function is a cursor. By default, the first time it returns the first 101 documents or 1 MB of data in the result set, it depends on which condition is met first, if the data volume of the 101 documents is greater than 1 MB, the number of returned documents will be less than 101. After that, the cursor will continue to request data from the database after All cached documents are iterated, starting from the second batch, a maximum of 4 MB of documents can be returned each time until all documents in the result set are output. To better illustrate the above data query process, we will use an animation to simulate it. This indicates the database service. The client uses the find function to send a query request. We assume that the query results returned by the find function have a total of 10 documents, and these 10 results will be returned in batches, assume that five documents are returned each time. The five documents returned by the first batch of find functions will be cached in the memory, and the cursor variable will point to the first document. Then, we can use the cursor variable to operate the result set, outputs cached documents iteratively. After traversing the last document, the cursor will automatically send a getMore data request to the database service, and then iteratively output the returned results, until all documents in the result set are completely output. The advantage of batch return results is that, on the one hand, it can reduce the number of client connections to the server, thus reducing the query burden on the server. On the other hand, when the query result set is large, this batch return method can reduce client latency and improve data processing efficiency. It should be noted that, if you want to sort a field without an index, before returning the result, database Service needs to load all documents that meet the query conditions into the memory for sorting. If the result set is large, memory resources are greatly consumed. Therefore, indexing fields that are frequently used in documents not only improves the document query speed, but also reduces memory usage.
3. Detailed course videoThe video course link is as follows: [MongoDB Data Query (2)]
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.