2017-01-21 74 views
3

我有一個批量操作,其中我每隔2小時在MongoDB數據庫中插入10000個項目。該代碼看起來像這樣Mongodb批量插入連接超時

let bulk = models.Product.collection.initializeUnorderedBulkOp(); 
... 
    if (bulk.length > 0) { 
     bulk.find({ 
      "$or": [ 
       { 
        "updatedAt": { 
         "$lt": timestamp 
        } 
       }, 
       { 
        "discount": { 
         "$eq": 0 
        } 
       } 
      ] 

     }).remove() 
     bulk.execute((error, result) => { 
      if (error) { 
       console.error('Error while inserting products' + JSON.stringify(error)) 
      } 
      else { 
       console.log('Successfully inserted ' + result.nInserted + ' upserted ' + result.nUpserted + ' matched ' + result.nMatched + ' modified ' + result.nModified + ' removed ' + result.nRemoved) 
      } 
     }) 
    } 
    else { 
     console.log('There were no bulk operations to execute ' + products.length) 
    } 
} 

我的連接狀態越來越超時。我對貓鼬連接選項看起來像這樣

let options = { 
    mongos: { 
     ssl: true, 
     sslValidate: true, 
     sslCA: ca, 
    } 
} 

我清楚地知道正在被其他計算器線程

server: { 
    socketOptions: { 
     keepAlive: 300000, 
     connectTimeoutMS: 30000 
    } 
} 

討論這方面的設置我讀的keepAlive和connectTimeoutMS但如何做的文檔我知道兩者的正確價值,我是否也需要socketTimeoutMS?

謝謝你的忠告提前

更新1

我不斷收到此錯誤:

{"name":"MongoError","message":"connection 0 to aws-ap-southeast-1-portal.2.dblayer.com:15284 timed out"} 

我的連接選項看起來像撰寫現在這個 //選項。 io數據庫

let options = { 
    mongos: { 
     ssl: true, 
     sslValidate: true, 
     sslCA: ca, 
    }, 
    server: { 
     socketOptions: { 
      keepAlive: 300000, 
      connectTimeoutMS: 300000 
     } 
    }, 
    replset: { 
     socketOptions: 
     { 
      keepAlive: 300000, 
      connectTimeoutMS: 300000 
     } 
    } 
} 

回答

1

好吧,我有一批100,而不是1000降低了我的批量操作,它做工精細現在.. 它運作良好,1000當我在做:

bulk.insert(programme); 

但現在,我這樣做,並它不適用於1000 /散裝:

bulk.find({ eventId: programme.eventId }).upsert().replaceOne(programme);