Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Basic practice of Mongodb (2)

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

In the previous article mainly introduced the MongoDB documents, collections, databases and other operations and the document add, delete, change related knowledge, and then will summarize some of the relevant knowledge of the query.

In MySQL, we know that data query is the main content of optimization, and technologies such as read-write separation can be used to deal with database query optimization, which is sufficient to see that database query is a very important part of every system. The simple use of find was introduced before, and some relatively complex queries will be introduced below.

I. data query

In MySQL database, the query of data is mainly realized by select combined with where clause, which is particularly powerful, such as multi-table joint query, supporting regular expressions and so on. Don't make too many introductions here. This paper mainly introduces the related query of MongoDB. In MongoDB, data query is mainly realized by find (). At the same time, some conditions can also be used.

1.1 display single piece of data

The use of find () was mentioned in the previous article, but every time the data is queried, it is owned by the query, shows part of it, and can be iterated with it. Sometimes we want to query one of the data, and the specific operation should be implemented according to the specific requirements.

Syntax for MongoDB query data

Db.collection.find (query, projection)

Query: optional, use the query operator to specify query conditions

Projection: optionally, use the projection operator to specify the returned key. When querying, all the key values in the document are returned, simply omitting the parameter (default omitted). You can use the pretty () method to read data in an easy-to-read way, with the following syntax format

> db.col.find () .pretty ()

The pretty () method displays all documents in a formatted manner.

For example:

Db.winner.find () .pretty () {"_ id": ObjectId ("592e7d1caaa464fa8a557e95"), "winne": 1955} {"_ id": ObjectId ("592e7d1eaaa464fa8a557e96"), "winne": 1955} {"_ id": ObjectId ("592e7d1faaa464fa8a557e97"), "winne": 1955} {"_ id": ObjectId ("592e7d1faaa464fa8a557e98"), "winne": 1955} {"_ id": ObjectId ("592e7d21aaa464fa8a557e99") "winne": 1955} {"_ id": ObjectId ("592e7d22aaa464fa8a557e9a"), "winne": 1955} {"_ id": ObjectId ("592e7e14aaa464fa8a557ec4"), "winne": 41} {"_ id": ObjectId ("592e7e14aaa464fa8a557ec5"), "winne": 42} {"_ id": ObjectId ("592e7e14aaa464fa8a557ec6"), "winne": 43} {"_ id": ObjectId ("592e7e14aaa464fa8a557ec7"), "winne": 44}

1. Query all the data in a collection

Db.winner.find () {"_ id": ObjectId ("592e7d1caaa464fa8a557e95"), "winne": 1955} {"_ id": ObjectId ("592e7d1eaaa464fa8a557e96"), "winne": 1955} {"_ id": ObjectId ("592e7d1faaa464fa8a557e97"), "winne": 1955} {"_ id": ObjectId ("592e7d1faaa464fa8a557e98"), "winne": 1955} {"_ id": ObjectId ("592e7d21aaa464fa8a557e99") "winne": 1955} {"_ id": ObjectId ("592e7d22aaa464fa8a557e9a"), "winne": 1955} {"_ id": ObjectId ("592e7e14aaa464fa8a557ec4"), "winne": 41} {"_ id": ObjectId ("592e7e14aaa464fa8a557ec5"), "winne": 42} {"_ id": ObjectId ("592e7e14aaa464fa8a557ec6"), "winne": 43} {"_ id": ObjectId ("592e7e14aaa464fa8a557ec7") "winne": 44} {"_ id": ObjectId ("592e7e14aaa464fa8a557ec8"), "winne": 45} {"_ id": ObjectId ("592e7e14aaa464fa8a557ec9"), "winne": 46} {"_ id": ObjectId ("592e7e14aaa464fa8a557eca"), "winne": 47} {"_ id": ObjectId ("592e7e14aaa464fa8a557ecb"), "winne": 48} {"_ id": ObjectId ("592e7e14aaa464fa8a557ecc") "winne": 49} {"_ id": ObjectId ("592e7e14aaa464fa8a557ecd"), "winne": 50} {"_ id": ObjectId ("592e7e14aaa464fa8a557ece"), "winne": 51} {"_ id": ObjectId ("592e7e14aaa464fa8a557ecf"), "winne": 52} {"_ id": ObjectId ("592e7e14aaa464fa8a557ed0"), "winne": 53} {"_ id": ObjectId ("592e7e14aaa464fa8a557ed1"), "winne": 54} Type "it" for more

20 pieces of data are displayed by default, and other data can be entered into it iteration.

2. Display a piece of data

Find () outputs all the results, and some documents may have the same content, but "_ id" is definitely different, so we can use the findOne () method to query, or we can use db.winner.find ({winne:1955}) .limit (1).

Db.winner.find ({winne:1955}) {"_ id": ObjectId ("592e7d1caaa464fa8a557e95"), "winne": 1955} {"_ id": ObjectId ("592e7d1eaaa464fa8a557e96"), "winne": 1955} {"_ id": ObjectId ("592e7d1faaa464fa8a557e97"), "winne": 1955} {"_ id": ObjectId ("592e7d1faaa464fa8a557e98"), "winne": 1955} {"_ id": ObjectId ("592e7d21aaa464fa8a557e99") "winne": 1955} {"_ id": ObjectId ("592e7d22aaa464fa8a557e9a"), "winne": 1955} if you want to query a piece of data from winne=1955 in the winner collection And use find () to query all the data. At this point, you can use findOne () db.winner.findOne ({winne:1955}) {"_ id": ObjectId ("592e7d1caaa464fa8a557e95"), "winne": 1955} or you can use db.winner.find ({winne:1955}) .limit (1) {"_ id": ObjectId ("592e7d1caaa464fa8a557e95"), "winne": 1955} the difference between the two is somewhat similar to distinct in MySQL, which returns the first result of the query. If the desired data is not found, the NULL,db.winner.findOne ({winne:200888}) nulldb.winner.find ({winne:1955}) .limit (1) method is the same as the limit in MySQL, mainly to limit the number of query results.

3. Query data that meet certain conditions

When querying in MySQL, you can query data with information such as where and fields, and it is also possible in MongoDB, and some conditional judgment statements can also be supported.

The similar statement in the format example RDBMS is equal to {:} db.col.find ({"winne": "1995"}). Pretty () where winne = '50' less than {: {$lt:}} db.col.find ({"winne": {$lt:50}) .pretty () where winne

< 50小于或等于{:{$lte:}}db.col.find({"winne":{$lte:50}}).pretty()where winne 50大于或等于{:{$gte:}}db.col.find({"winne":{$gte:50}}).pretty()where winne >

= 50 is not equal to {: {$ne:}} db.col.find ({"winne": {$ne:50}}) .pretty () where winne! = 50$ gt-greater than $gte-gt equal$lt-less than $lte-lt equal$ne-not equal1, query winne in the winner collection

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report