MongoDB count distinct value?(MongoDB计算不同的价值?)
问题描述
下面显示我的代码.我必须计算重复的不同值的次数.在这里,我在结果"中存储了不同的值.我使用 collection.count() 来计算,但它不起作用.请任何人告诉我我必须在哪里出错.非常感谢.
Here below show my code. I have to calculate the how many times distinct value repeated. Here i have store distinct value in "results".I used collection.count() to calculate but it's not work. please any one tell me where i have to mistake. Thank you very much .
var DistinctIntoSingleDB = function(Collection,arr,opt,distVal,callback){
Collection.find({}).distinct(distVal, function(err, results) {
if(!err && results){
console.log("Distinct Row Length :", results.length);
var a,arr1 = [];
for(var j=0; j<results.length; j++){
collection.count({'V6': results[j]}, function(err, count) {
console.log(count)
});
arr1.push(results[j]+ " : " +a);
}
callback(results,arr1);
}else{
console.log(err, results);
callback(results);
}
});
推荐答案
在集合 'col1' 上获取字段 'field1' 的不同值并写入单独的集合 'distinctCount'.如果集合很大,也允许使用磁盘空间.
To get occurrences of distinct values of a field 'field1' on a collection 'col1' and write to a separate collection 'distinctCount'. Also allow to use disk space in case the collection is huge.
db.col1.aggregate(
[{$group: {
_id: "$field1",
count: { $sum : 1 }
}}, {
$group: {
_id: "$_id",
count: { $sum : "$count" }
}},{
$out: "distinctCount"
}],
{allowDiskUse:true}
)
这篇关于MongoDB计算不同的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MongoDB计算不同的价值?


基础教程推荐
- Fabric JS绘制具有活动形状的多边形 2022-01-01
- 原生拖动事件后如何获取 mouseup 事件? 2022-01-01
- fetch 是否支持原生多文件上传? 2022-01-01
- 即使用户允许,Gmail 也会隐藏外部电子邮件图片 2022-01-01
- Bootstrap 模态出现在背景下 2022-01-01
- 在 contenteditable 中精确拖放 2022-01-01
- Bokeh Div文本对齐 2022-01-01
- npm start 错误与 create-react-app 2022-01-01
- 如何添加到目前为止的天数? 2022-01-01
- 检查 HTML5 拖放文件类型 2022-01-01