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计算不同的价值?


基础教程推荐
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 直接将值设置为滑块 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01