Nodejs mongodb在不实际获取光标的情况下获取文档大小

Nodejs mongodb fetching document size without actually fetching cursor

本文关键字:获取 情况下 文档 光标 mongodb 不实际 Nodejs      更新时间:2023-09-26

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

我已经研究了很多问题,比如这里,但我不想获取查询结果来了解它有多少KB

我只想要一些类似的东西:

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
}

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