1. find is used in MongoDB for query. You can specify the first parameter of find to perform all and partial queries. 1. querying all null query documents {} matches all the content of the set. If no query document is specified, the default query is {}. 2. Partial query 3. Filter key of the key is the key that you are interested in when querying.
1. find is used in MongoDB for query. You can specify the first parameter of find to perform all and partial queries. 1. querying all null query documents {} matches all the content of the set. If no query document is specified, the default query is {}. 2. Partial query 3. Filter key of the key is the key that you are interested in when querying.
1. find operation
MongoDB uses find for query. by specifying the first parameter of find, you can perform all and partial queries.
1. query all
The empty query document {} matches all the content of the set. If no query document is specified, the default query is {}.
2. Partial Query
3. Key Filtering
Key filtering means that only the key values of interest are returned during the query. This is achieved by specifying the second parameter of find. This reduces the amount of data transmitted and the time and memory consumption of client-side document decoding.
When querying, the document value that the database cares about must be a constant.
Ii. query Conditions
1. Comparison Query
$ Lt, $ lte, $ gt, $ gte, $ ne and <, <=, >,>= ,! = Is a one-to-one correspondence. They can be combined to find values in a range.
2. join query
$ In is used to query multiple values of a key. $ nin returns a document that does not match all the conditions in the filter array. Combining $ in and $ not can achieve the same effect as $ nin.
$ Or is used to query multiple keys.
Iii. Query of Specific Types
1. null Query
Null not only matches itself, but also matches documents without a key.
2. Regular Expression
3. array Query
$ All: matches the array with multiple elements.
$ Size: queries an array of the specified length.
$ Slice: returns a subset of arrays.
4. embedded document Query
4. $ where Query
$ Where query is the advanced query part of MongoDB. It can execute any JavaScript code as part of the query and is complementary to other query methods.
$ Where Query Needs to convert each document from BSON to a JavaScript Object and run it through the $ where expression. This process cannot use indexes, so the query speed is much slower than the regular query speed. If necessary, you can use the regular query as the pre-filter. If you can use the index, you can use the index to filter according to the non-$ where clause, and then use $ where to optimize the result. Another method is map Simplification-MapReduce.
5. cursor
A cursor is very useful. MongoDB uses a cursor to return the execution result of the find statement. The client can use cursors to effectively control the final results, such as paging and sorting.
1. limit, skip, and sort
Limit: limit the number of results.
Skip: skips the first n matching documents and returns the remaining documents. Performance problems may occur when the skip is skipped too many documents. We recommend that you avoid this problem as much as possible.
Sort: sort documents by specified keys. 1 is in ascending order and-1 is in descending order.
2. Advanced query options
Packaging query: Use sort, limit, or skip to further control the final result.
Useful configuration options:
$ Maxscan: integer indicates the maximum number of scanned documents to be queried.
$ Min: document, the start condition of the query.
$ Max: document, the end condition of the query.
$ Hint: document, which specifies the index used by the server for query.
$ Explain: boolean, used to obtain the query execution details (indexes, number of results, time consumed, etc.), rather than actually executing the query.
$ Snapshot: boolean, which ensures that the query result is a consistent snapshot at the moment when the query is executed, to avoid inconsistent reading.
The query condition is encapsulated in a larger query document. For example, when the following query is executed:
db.foo.find({"name":"bar"}).sort("x":1)
Shell converts the query from {"name": "bar"} to {"$ query": {"name": "bar"}, "$ orderby ": {"x": 1} instead of directly sending {"name": "bar"} to the database as a query document.
3. Inside the cursor
Two ways to view the cursor: client cursor and database cursor represented by client cursor (server ).
On the server side, the cursor consumes memory and other resources, so release the cursor as soon as possible under reasonable circumstances. The server causes the cursor to terminate as follows:
1. The cursor is automatically cleared when the matching result iteration is completed.
2. When the client is no longer in the scope, the driver will send a special message to the server to destroy the cursor.
3. Time-out destruction. You can use the immortal function to close the cursor timeout. To use this operation, you must close the cursor after the iteration ends.
Vi. Summary
To use MongoDB, you must properly design the document structure to meet specific requirements. For example, if a document is randomly selected and skip is used to skip a random document, no random key is added to the document. Then, a random number is used to query the document efficiently. The random key can also be indexed, which is more efficient. Reasonable choice and design.