Mongodb 4.0 bulkWrite 在更新时非常慢

Jen*_*er 5 performance mongodb node.js bulkupdate

我有一个拥有超过 100 万用户的集合,我正在尝试更新某些事件的用户余额。

当我尝试更新例如 299 行时,它最多需要 15739.901 毫秒

服务器上没有高负载,它只是运行 mongo。我将数据库存储在 SSD Samsung evo 860 上,但 MongoDB 安装在 HDD 上。

这是我的功能:

async usersUpdate(usersToUpdate){
const updates = [];
   return new Promise(async (resolve, reject) => {
       users.forEach(user=>{

            updates.push(
               { "updateOne": {
                    "filter": { "userID": user.userID, 'balance':user.userBalance },
                    "update": { "$set": { "user.$.userBalance": user.newBalance } , "$addToSet":{'orders.$.orderID':user.OrderID} }
                }

               });

       }


       console.log('total updates' , updates.length);
       if (updates.length > 0){
           const DbConnection = await getConnection();
           const usersTable = DbConnection.collection('usersCollection');
           transactionsTable.bulkWrite(updates, {"ordered": false, writeConcern : { w : 0 } }, function(err, result) {
               // do something with result
               if (err) return reject(err);
               return resolve(result)

           });
       }else{
           return resolve('Nothing to update');
       }
   });
}
Run Code Online (Sandbox Code Playgroud)

userID 和 userBalance 都被编入索引,我将writeconcernequals设置为 false。

我不知道代码有什么问题以及为什么它超级慢。

有什么问题,我怎样才能加快进度?

mongodb 配置文件:

storage:
  dbPath: "/ssd/mongodb"
  journal:
    enabled: false
Run Code Online (Sandbox Code Playgroud)

解释结果:

{ queryPlanner: 
   { plannerVersion: 1,
     namespace: 'usersDB.usersCollection',
     indexFilterSet: false,
     parsedQuery: 
      { '$and': 
         [ { userID: 
              { '$eq': 'Kfasg3ffasg' } },
           { 'user.userBalance': { '$eq': 10 } } ] },
     winningPlan: 
      { stage: 'FETCH',
        filter: { 'user.userBalance': { '$eq': 10 } },
        inputStage: 
         { stage: 'IXSCAN',
           keyPattern: { userID: 1 },
           indexName: 'userID_1',
           isMultiKey: false,
           multiKeyPaths: { userID: [] },
           isUnique: true,
           isSparse: false,
           isPartial: false,
           indexVersion: 2,
           direction: 'forward',
           indexBounds: 
            { userID: 
               [ '["Kfasg3ffasg", "Kfasg3ffasg"]' ] } } },
     rejectedPlans: [] },
  executionStats: 
   { executionSuccess: true,
     nReturned: 1,
     executionTimeMillis: 24,
     totalKeysExamined: 1,
     totalDocsExamined: 1,
     executionStages: 
      { stage: 'FETCH',
        filter: { 'user.userBalance': { '$eq': 10 } },
        nReturned: 1,
        executionTimeMillisEstimate: 0,
        works: 2,
        advanced: 1,
        needTime: 0,
        needYield: 0,
        saveState: 0,
        restoreState: 0,
        isEOF: 1,
        invalidates: 0,
        docsExamined: 1,
        alreadyHasObj: 0,
        inputStage: 
         { stage: 'IXSCAN',
           nReturned: 1,
           executionTimeMillisEstimate: 0,
           works: 2,
           advanced: 1,
           needTime: 0,
           needYield: 0,
           saveState: 0,
           restoreState: 0,
           isEOF: 1,
           invalidates: 0,
           keyPattern: { userID: 1 },
           indexName: 'userID_1',
           isMultiKey: false,
           multiKeyPaths: { userID: [] },
           isUnique: true,
           isSparse: false,
           isPartial: false,
           indexVersion: 2,
           direction: 'forward',
           indexBounds: 
            { userID: 
               [ '["Kfasg3ffasg", "Kfasg3ffasg"]' ] },
           keysExamined: 1,
           seeks: 1,
           dupsTested: 0,
           dupsDropped: 0,
           seenInvalidated: 0 } },
     allPlansExecution: [] },
  serverInfo: 
   {  }
Run Code Online (Sandbox Code Playgroud)