arrays - mongo native addToSet not working when update 2 fields at the same time -
mongo native addtoset not working when update 2 fields @ same time
this way not ok
db.col.findandmodify( {_id: 'abcxyz123'} ,[['_id','descending']] ,{$addtoset:{field_1: 'aaa'}, $addtoset:{field_2: 'aaa'}} ,{new: true}, function(err, result) { console.log(result.value) //field_1: [], field_2: ['aaa'] //it should field_1: ['aaa'], field_2: ['aaa'] });
this way works ok
db.col.findandmodify( {_id: 'abcxyz123'} ,[['_id','descending']] ,{$addtoset:{field_1: 'aaa'}} ,{new: true}, function(err, result) { console.log(result.value) //field_1: ['aaa'], field_2: [] //it ok });
you doing wrong syntax $addtoset
is:
{ $addtoset: { <field1>: <value1>, ... } }
so here is:
db.col.findandmodify( ... { $addtoset: { field_1: 'aaa', field_2: 'aaa' }} ... });
Comments
Post a Comment