MongoDB 选择计数组

编程入门 行业动态 更新时间:2024-10-09 16:29:37
本文介绍了MongoDB 选择计数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在玩 MongoDB,试图弄清楚如何做一个简单的

I am playing around with MongoDB trying to figure out how to do a simple

SELECT province, COUNT(*) FROM contest GROUP BY province

但我似乎无法使用聚合函数来计算它.我可以使用一些非常奇怪的组语法来做到这一点

But I can't seem to figure it out using the aggregate function. I can do it using some really weird group syntax

db.user.group({ "key": { "province": true }, "initial": { "count": 0 }, "reduce": function(obj, prev) { if (true != null) if (true instanceof Array) prev.count += true.length; else prev.count++; } });

但是使用聚合函数有更简单/更快的方法吗?

But is there an easier/faster way using the aggregate function?

推荐答案

这将是使用 聚合:

This would be the easier way to do it using aggregate:

db.contest.aggregate([ {"$group" : {_id:"$province", count:{$sum:1}}} ])

更多推荐

MongoDB 选择计数组

本文发布于:2023-10-26 06:34:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1529363.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   MongoDB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!