Hi, Can anybody told me the Equivalent MongoDB query for the following RDBMS sql query

瀏覽次數:64 次
跳到第一則未讀訊息

Aditya Kumar

未讀,
2017年9月19日 上午8:46:212017/9/19
收件者:mongodb-user
I have searched a lot in interent to find out the MongoDB code for the following SQL query. Can anybody help me out with this

SELECT country, count(*) as total_employees, sum(salary) as total_sal
FROM employee
group by country 
having avg(salary) >= 10000;

Please don't tell me with db.employee.group() function because it's now deprecated. Looking for solution with db.employee.aggregate() function.

Rhys Campbell

未讀,
2017年9月20日 清晨6:08:452017/9/20
收件者:mongodb-user
Try this;

db.employee.aggregate([
{ "$group": { "_id": "$country", "total_employees": { "$sum": 1 }, "total_sal": { "$sum": "$salary" }, "avg_sal": { "$avg": "$salary" } }},
{ "$match":  { "avg_sal": { "$gt": 10000 } } }
]);

It's always worth providing a few example documents. You might get a quicker answer.

Rhys


sourav biswas

未讀,
2017年9月20日 下午6:29:052017/9/20
收件者:mongodb-user
also do double grouping in mongodb if there is a need to do something like count (distinct <field>)
回覆所有人
回覆作者
轉寄
0 則新訊息