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

How to query promotion for MONGODB

2025-04-05 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 query the promotion of MONGODB, many people may not know much about it. In order to make you understand better, the editor summed up the following contents for you. I hope you can get something according to this article.

Recently, the use of MONGODB is becoming more and more "unhurried". There are more and more ideas on a website about the partial replacement of traditional database tables by MONGODB. In fact, there are many things that mongoDB can do, but the current problem is that developers may still be in their infancy to use MONGODB. In addition, some query statements from SQL to MONGO FIND may not be familiar with. In fact, the writing of MONGO FIND is not complicated. Some SQL's advanced query MONGODB is mostly no problem, and most of the problems are caused by people designing MONGODB's collection, but this is not a problem. Add a field, 7 hours 24 hours, for other business systems, add a field to consider a lot of problems, while most of MONGO do not have to consider, this is gratifying.

In the last issue, the MONGODB query in this issue has to be a little more complicated.

If you are interested, you can take a look at the overall format specification of JSON. It will be OK in an hour.

First of all, we have a query data base, so that when we discuss it, we can say

The above is the data for discuss this time, but not this one, of course, it only shows the general format

According to the previous issue,

Requirement 1 needs to query the data with a qty of 50. The query statement on the way is very clear. The braces in the curly braces are enclosed in square braces, and the key values are enclosed in double quotation marks. Then follow the data you want to query.

The traditional way of writing SQL such as select * from inventory where qty = 50

Requirement 2 traditional DBA will say something to the above writing method, cannot use *, which field should be written, MONGO is the same, please tightly display qty in the query results other fields are not displayed, the writing method is to add a conditional filtering method after the query, the field mark 1 that needs to be displayed does not need to be displayed.

Select qty from inventory where qty = 50

Requirement 3 displays data with H attribute 10 in the SIZE field

Select item,qty,size from inventory where size.h = 10

If there is nesting in the data, you need to start with the node at the root

Requirement 4 We query data with SIZE.H greater than or equal to 10 and ITEM is PLANNER

Select item,qty,size.h,size.w from inventory where size,h > = 10 and item = "planner"

Requirement 5 has some requirements that do not exist in the traditional database, you can query whether the field is NULL, empty, but you have solidified table SCHEMA, you will not ask whether the data field of each row exists, but the field of MONGODB may not exist in each document. So the following statement is not comparable in a traditional database

The following query is to query the specific records of the existence of the KEY status in the inventory collection, of course, you can also find out how many docuemnts exist in the number of collection.

Calculate the number of status in the collection and the number of documents of the KEY

Must be expressed in the traditional SQL statement, select count (*) from inventory as a where status is not null (can only be close, because its practical IS NOT NULL expression is not accurate)

Requirement 6 if we want to query a range of values, how to query, there are some reminders in this DEVELOPER, even if it is the SCHEMA of MONGODB, you can't think of an out, if the value of your later query is queried in the way of RANGE, it is recommended to use non -, "" double quotation marks characters to carry out, so later processing, the performance is not very good.

Select item,qty,size.h,size.w from inventory where qty between 45 and 50

Requirement 7 IN is often used in traditional queries, how to express it in MONGODB way

Select item,qty,size.h,size.w from inventory where qty in (45pl 50)

Requirements 8 sort and output the first few pieces of data

Limit the amount of data displayed. Add .limit (display quantity) at the end, like limit and ORACLE rownum of SQL SERVER top and mysql.

The method of sorting is also very simple, add sort ({the field you want to sort and whether you are in positive or reverse order}) after the query statement, and it is recommended not to sort if it is not necessary.

Requirement 9 finally, let's take a look at a less common document, which is a data storage method that contains nested data in an array. Regardless of whether this is good or not, our requirement now is to query the record that type2 is IRON in TAGS.

Here is the format of the document

The query method requires a special elemMatch query method to query such requirements

After reading the above, do you have any further understanding of how to query the promotion of 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.

Share To

Internet Technology

Wechat

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

12
Report