Multiple Document Upsert in Mongoose
The bulk upsert is not really possible as mentioned over here, along with a possible solution.
However, you can consider simpler approach of using upsert
operation for each poolItems
with promise or some async method like each. The below code should work:
var async = require('async');var Pool = require('/path/to/pool.js');var poolItems = [{document_id :"FBIS3-50136" ,topic_id :"301" , project :"A1"},{document_id :"LA040190-0178" ,topic_id :"302" , project :"A1"},{document_id :"FT934-5418" ,topic_id :"303" , project :"A1"},{document_id :"LA071090-0047" ,topic_id :"304" , project :"A1"}];async.each(poolItems, function(poolItem, callback){ Pool.findOneAndUpdate({document_id: poolItem.document_id}, poolItem, {upsert:true}, function(err, doc){ if(err){ return callback(err); } return callback(); });}, function(err){ if(err){ console.log(err); } else{ console.log("All pool items have been upserted!") }});