Reputation: 171
Hello here is my problem,
var poolSchema = mongoose.Schema({
"topic_id": {
type: Number,
default: null,
required: true
},
"document_id": {
type: String,
default: null,
required: true
},
"project":{
type:String,
default: false,
required: true
},
"createddate":{
type:Date,
default : Date.now
}
}, { collection: "sorguHavuzu" });
i have an array of pool documents which each item has different field values such below :
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"}]
Here is my scheme :
I would like to upsert the items in the array by document_id field. So here is my update operation.
var query = {"document_id" : { $in:["FBIS3-50136","LA040190-0178","FT934-5418","LA071090-0047"]}};
Pools.collection.update(query, { $push : { "$ROOT" : poolItems } }, { upsert: true, multi : true}, callback);
Error: The dollar ($) prefixed field \'$ROOT\' in \'$ROOT\' is not valid for storage.
But on every attempt, i got different errors, is there a way to upsert items with mongoose update operation ? Thanks
Upvotes: 3
Views: 2257
Reputation: 4619
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!")
}
});
Upvotes: 3