简体   繁体   中英

how it works with mongoose multiple update?

Array;

 arr=[
  {
  id: [ '5e6e9b0668fcbc7bce2097ac', '5e6e9b0e68fcbc7bce2097af' ],
  color: [ 'a', 'b' ]
  }
 ]

Models;

const varyant = Models.varyant

function;

Promise.all(

 arr.map((item)=>{ 

 return varyant.updateMany({"_id": item.id }, {"$set":   { "color":"value" }  }, 

 {multi:true}); 

 })).then(function(results){

 });

this function success, write color:"value"

but change

 return varyant.updateMany({"_id": item.id }, {"$set":{ "color":{ $in: arr.color } }  },

nodejs output:

 (node:62738) UnhandledPromiseRejectionWarning: CastError: Cast to string failed for value "{ '$in': [ 'a', 'b' ] }" at path "color"

Where am I making a mistake?

$in is not a insert operator is a select operator

You could change the arr array with some easy suitable format

 const arr = [{ id: ['5e6e9b0668fcbc7bce2097ac', '5e6e9b0e68fcbc7bce2097af'], color: ['a', 'b'] }]; const new_arr = arr[0].id.map((id,i)=>({id,color:arr[0].color[i]})); //[{id,color}] Promise.all( new_arr.map((item, i) => { return varyant.updateMany({ "_id": item.id }, { "$set": { "color": item.color, } }, { multi: true }); })).then(function(results) { });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM