Nodejs mongodb 获取文档大小而不实际获取游标 [英] Nodejs mongodb fetching document size without actually fetching cursor

查看:42
本文介绍了Nodejs mongodb 获取文档大小而不实际获取游标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是,如何在不实际获取的情况下获得 cursor 大小(以 KB 为单位)?

My question is, how can I get a cursor size (in KBs) without actually fetching it ?

我已经检查了很多问题,例如 here 但我不想获取查询结果以了解 KB 是多少.

I've already examined a lot of question such as here But I don't want to fetch query result to learn how much KB is it.

我只想要这样的东西:

var MongoClient = require('mongodb').MongoClient,
  test = require('assert');
MongoClient.connect('mongodb://localhost:27017/test', function(err, db) {

  var collection = db.collection('simple_query');

  // Insert a bunch of documents for the testing
  collection.insertMany([{a:1}, {a:2}, {a:3}], {w:1}, function(err, result) {
    test.equal(null, err);

    collection.find(/**SOME QUERY*/).size(function(err, SIZE) {
      test.equal(null, err);
      test.equal(32111351, SIZE); // in bytes or kilobytes whatever
      db.close();
    });
  });
});

推荐答案

类似的事情?

var avgSize = db.collectionName.stats().avgObjSize;

// ...

collection.count(/* some query */, function(err, count) {
    var approximateSize = count*avgSize; // This could work for simple database models
}

我知道它并不完美,但这是我找到的最好的方法.

I know its not perfect, but it is the best way i found.

这篇关于Nodejs mongodb 获取文档大小而不实际获取游标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆