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

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -