In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
1. MongoDB data query
(1) MongoDB uses find function to query data, which has the same function as select function in SQL, and can provide many functions similar to relational database, including mapping, sorting and so on.
Db. Collection name. Find (query,fields,limit,skip)
1.query is equivalent to the where statement in SQL.
2.fields is used for field mapping. When 1 means that 0 is not displayed, the syntax format: {field:0} or {field:1}, use either all 0 or all 1. Mixed use except "_ id": 0 is wrong.
3.limit limits the number of documents in the query result set.
4.skip skips the result of a certain amount of data and sets the offset of the first returned document.
(2) MongoDB and precautions
1.MongoDB does not support join queries between multiple collections, and the find function can only query one collection at a time.
When the 2.find parameter is empty or the query condition is an empty document, all documents in the collection are returned.
3. In addition to taking limit and skip as arguments to the find function, you can also use the limit and skip functions alone to decorate the query results.
For example: db.student.find ({age: {$lt:22}}) .limit (5) .skip (10)
4. The returned query result set is unordered by default, and if you need to sort the results, you can use the sort function. 1 means ascending order-1 is descending order.
For example: db.student.find () .sort ({name:1,age:-1})
5.db.collection.findOne () returns only the first piece of data.
6. When the number of collection documents of the query is very large, an index can be created to speed up the query of the data.
7. In addition to implementing basic queries using find functions, MongoDB provides an aggregation framework for complex queries.
Introduction and use of query operators
(1) compare query operators
(2) logical query operator
Logical query operator, which can join multiple query conditions for logical and, OR, no, and reverse operations.
(3) element operator
The element query operator, which is used to query the properties of a field in a document, including whether the field exists and the data type of the field.
Third, the cursor of MogoDB
When querying with the find function, MongoDB does not return all the documents in the result set at once, but returns the results in batches. The amount of data returned will not exceed 4m. By default, 101m documents or 1m data of the result set will be returned (depending on which condition is met first). The returned document is cached in memory, and a cursor cursor points to the first one in the batch result set, and when the cursor traverses the last document, the cursor automatically sends an getMore data request to the MongoDB database to get the next batch of result sets.
(1) pay attention to the following problems when using cursors.
1. When the find function is called, shell does not query the database immediately, but waits for the actual use of the hasNext of the cursor (when the result is obtained) before sending the query request.
two。 Almost every method of the cursor object returns the cursor object itself, so it is convenient to make chain function calls.
3. If the cursor returned by the find function is not assigned to a local variable to save, the cursor will automatically iterate 20 times by default. If you want to iterate manually, you must assign a value to a local variable and then iterate with the provided function.
(2) the life cycle of cursors (creation, use and destruction)
When the client initiates a query request to the server using the find function, a cursor is created on the server side, and then the cursor function can be used to manipulate the query results.
The following three situations cause cursors to be destroyed:
1. After the cursor traversal is complete, or the client initiatively sends a termination message.
two。 The cursor variable saved by the client is not in scope.
3. The cursor was not manipulated within 10 minutes on the server side.
(3) Vernier snapshot
Because cursors are not isolated, cursors may return documents that have been moved to the end of the collection because of their larger size. The solution to this problem is to take a snapshot of the query. The syntax is as follows:
Db.collection.find () .snapshot ()
After using the snapshot, the query is traversed on the _ id index, which ensures that each document is returned only once, thus ensuring the consistency of the results obtained. However, snapshots can slow down queries, so you need to be careful when using them.
Fourth, the use of findAndModify function
Comparison of findAndModify and update functions:
1. Update and modify documents in an atomic way
two。 When multiple documents meet the query condition:
① findAndModify uses the sort option to sort the results and select the first document.
② update cannot choose which document to update.
3.update can modify more than one document at a time, but not findAndModify.
4.update can set the security level for writing. FindAndModify is not allowed.
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.