簡體   English   中英

Node.js mongodb在不實際獲取游標的情況下獲取文檔大小

[英]Nodejs mongodb fetching document size without actually fetching cursor

我的問題是,如何不實際獲取cursor情況下獲得其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
}

我知道它並不完美,但這是我發現的最好方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM