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 calculate subquery in MongoDB

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article introduces how to calculate the sub-query in MongoDB. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

It is true that MongoDB is stronger than relational databases in some respects (such as the ability to handle additional log data), but it is weaker in terms of structured computing power. For example, MongoDB does not support subqueries, so when you encounter these complex operations, you can only read out the data first and then calculate it, and it is not very easy to write such calculations in languages such as Java.

For example, to deal with a scenario where the order information is found, the SELLERID in the order must be the employee id of STATE= California in the employee collection. If it is written as sql, it means:

Select * from orders where orders.sellerid in (select eid from employee where employee.state='California').

The large amount of data in orders can not be taken out at once, the amount of data in employee is small, and the amount of data in the final result is also small. Some of the data are as follows:

MongoDB Collection orders:

...

{"_ id": ObjectId ("5434f88dd00ab5276493e270"), "ORDERID": 1, "CLIENT": "UJRNP"

"," SELLERID ": 17," AMOUNT ": 392," ORDERDATE ":" 15:28 on 2008-11-2 "}

{"_ id": ObjectId ("5434f88dd00ab5276493e271"), "ORDERID": 2, "CLIENT": "SJCH"

, "SELLERID": 6, "AMOUNT": 4802, "ORDERDATE": "15:28 on 2008-11-9"}

{"_ id": ObjectId ("5434f88dd00ab5276493e272"), "ORDERID": 3, "CLIENT": "UJRNP"

"," SELLERID ": 16," AMOUNT ": 13500," ORDERDATE ":" 15:28 on 2008-11-5 "}

{"_ id": ObjectId ("5434f88dd00ab5276493e273"), "ORDERID": 4, "CLIENT": "PWQ"

"SELLERID": 9, "AMOUNT": 26100, "ORDERDATE": "15:28 on 2008-11-8"}

...

MongoDB Collection employee:

...

{"_ id": ObjectId ("5437413513bdf2a4048f3480"), "EID": 1, "NAME": "Rebecca", "

SURNAME ":" Moore "," GENDER ":" F "," STATE ":" California "," BIRTHDAY ":" 1974-1 "

1-20 "," HIREDATE ":" 2005-03-11 "," DEPT ":" rotated "," SALARY ": 7000}

{"_ id": ObjectId ("5437413513bdf2a4048f3481"), "EID": 2, "NAME": "Ashley", "S"

URNAME ":" Wilson "," GENDER ":" F "," STATE ":" New York "," BIRTHDAY ":" 1980-07-

19 "," HIREDATE ":" 2008-03-16 "," DEPT ":" Finance "," SALARY ": 11000}

{"_ id": ObjectId ("5437413513bdf2a4048f3482"), "EID": 3, "NAME": "Rachel", "S"

URNAME ":" Johnson "," GENDER ":" F "," STATE ":" New Mexico "," BIRTHDAY ":" 1970-

12-17 "," HIREDATE ":" 2010-12-01 "," DEPT ":" Sales "," SALARY ": 9000}

...

Calculation results:

It is much easier with the help of the aggregator. It is a programming language specially designed for structured and semi-structured data computing. Query, sub-query, group filtering and so on all have function class libraries that can make up for the lack of structured computing power of MongoDB. For example, the above question can be solved in four lines:

A

one

= mongo_open ("mongodb://localhost:27017/test?user=test&password=test")

two

= mongo_shell (A1, "orders.find (, {_ id:0})")

three

= mongo_shell@x (A1, "employee.find ({STATE:'California'}, {_ id:0})") .fetch ()

four

= A2.select (A3. (EID) .sort () .pos@b (SELLERID)) .fetch ()

In fact, there are many situations where it is not convenient to use MongoDB to deal with structured computing, but it is very simple with the aid of the aggregator SPL. If you are interested, you can refer to: play with Mongo computing, simplify MongoDB association operations, and assist MongoDB computing.

The aggregator is also easily embedded in Java applications, and there are ways to use and get it how Java invokes the SPL script.

On how to calculate the sub-query in MongoDB to share here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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