我在mongodb有问题.
我想创建聚合女巫结果将是这样的:
A 10 B 2 C 4 D 9 E 3 ...
words
我的表中有一列,我想根据列的第一个字符对记录进行分组words
.
我找到了决心sql
但不是mongo.
我将非常感谢你的帮助
您没有显示集合中的文档,但您可以使用aggregate
集合方法执行此操作:
// Group by the first letter of the 'words' field of each doc in the 'test'
// collection while generating a count of the docs in each group.
db.test.aggregate({$group: {_id: {$substr: ['$words', 0, 1]}, count: {$sum: 1}}})