當(dāng)我們需要計(jì)算數(shù)據(jù)庫(kù)數(shù)量時(shí)缴渊,會(huì)用到db.collection.countDocuments(<query>, <options>)
let count = await db.collection.countDocuments({})
當(dāng)數(shù)據(jù)量非常大覆获,例如百萬(wàn)級(jí)別以上帆赢,而且檢索范圍非常寬廣時(shí)下硕,效率就會(huì)非常低下,甚至極大概率會(huì)time out而失敗羽资。
MongoNetworkTimeoutError: connection 4 to x.x.x.x:x timed out
在大數(shù)據(jù)的情況下淘菩,查詢緩慢是在所難免的,但希望花了時(shí)間要起碼有結(jié)果屠升。
db.collection.countDocuments(<query>, <options>)
有2個(gè)參數(shù)潮改,第二個(gè)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)
...
//因?yàn)闀r(shí)間可能會(huì)很長(zhǎng),當(dāng)需要中斷任務(wù)但不再斷進(jìn)程時(shí):
event.isStop = true
同樣的汇在,db.collection.count(<query>,<options>)
也可以用此方法翰萨。