當(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>) 也可以用此方法。