Mongo聚合:对BinData字段的字节数求和 [英] Mongo aggregates: sum the number of bytes of BinData field
本文介绍了Mongo聚合:对BinData字段的字节数求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个包含BinData的集合.这是一个示例文档:
I have a collection which contains a BinData. Here is an example doc :
- _id:整数
- origLength:int(未压缩大小)
- uploadDate:ISODate
- 数据:(应用程序级别的压缩数据)
我要执行一些尺寸检查.
I want to perform some size check.
我能够对未压缩的大小求和,我可以看到mongo db的大小.但是我没有明显的方法来计算数据字段的总大小.我找不到任何返回BinData大小的聚合函数.
I'm able to sum the uncompressed size, I can see the mongo db size. But I have no obvious way to count the total size of the data field. I can't find any aggregate function that return the BinData size.
db.passage.aggregate([{
$group: {
_id: 1,
count:{$sum : 1},
totalBytes : {$sum : "$origLength"},
compressedSize: {$sum : $XXX("$data")}}
}
]);
推荐答案
聚合目前还没有运营商进行此操作.
这是使用服务器端js代码的一种选择.
aggregate hasn't the operator to do this currently.
It's an option to use server-side js code.
db.eval(function() {
var len = 0;
db.passage.find({},{_id: 0, data: 1}).forEach(function(doc) {
var b = doc.data;
if (b instanceof BinData) {
len += b.length();
}
});
return len;
});
这篇关于Mongo聚合:对BinData字段的字节数求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文