MongoDb的countDocuments()優(yōu)化

當(dāng)我們需要計算數(shù)據(jù)庫數(shù)量時,會用到db.collection.countDocuments(<query>, <options>)

let count = await db.collection.countDocuments({})

當(dāng)數(shù)據(jù)量非常大,例如百萬級別以上,而且檢索范圍非常寬廣時,效率就會非常低下,甚至極大概率會time out而失敗。

MongoNetworkTimeoutError: connection 4 to x.x.x.x:x timed out

在大數(shù)據(jù)的情況下,查詢緩慢是在所難免的,但希望花了時間要起碼有結(jié)果。

db.collection.countDocuments(<query>, <options>)有2個參數(shù),第二個options文檔是:

  • limit integer Optional. The maximum number of documents to count.
  • skip integer Optional. The number of documents to skip before counting.
  • hint string or document Optional. An index name or the index specification to use for the query.
  • maxTimeMS integer Optional. The maximum amount of time to allow the count to run.

我們可以利用limit和skip,多次查詢相加得到結(jié)果:

let count = await db.collection.countDocuments({},{limit:10000,skip:Number})

NodeJS的例子:

async function getCount (collection, query={}, limit=10000, obj={}) {
  let skip = 0
  let total = 0
  while (true) {
    if (obj.isStop) break
    let count = await collection.countDocuments(query, { limit, skip })
    total += count
    skip += limit
    console.log('getCount:', total)
    if (count < limit) break
  }
  obj = void 0
  return total
}

基礎(chǔ)使用方法:

let count = await getCount(collection)

調(diào)整limit和控制中斷:

let event = {isStop:false}
let count = await getCount(collection,{},50000,event)

...
//因為時間可能會很長,當(dāng)需要中斷任務(wù)但不再斷進程時:
event.isStop = true

同樣的,db.collection.count(<query>,<options>) 也可以用此方法。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容