mongoose - Mongodb : array element projection with findOneAndUpdate doesn't work? -


i using mongoose , i'm trying update array element , updated. document structure :

{   name:string,     friends:[         {   name:string,             age:number         }     ] } 

when execute following query friends in result want 25 year old friends :

thecollection.findoneandupdate(     {   name : 'cherif',         'friends.name':'kevin'     },     {   $set:{             'friends.$.age':25         }     },     {   friends:         {   $elemmatch:             {   age : 25 }          }     },     function(err,result){         if (!err) {             console.log(result);         }}); 

as the docs findoneandupdate specify, need include projection object select property of options parameter:

thecollection.findoneandupdate(     {   name : 'cherif',         'friends.name':'kevin'     },     {   $set:{             'friends.$.age':25         }     },     {   select: {              friends: {                $elemmatch:                 {   age : 25 }              }         }     },     function(err,result){         if (!err) {             console.log(result);         }     }); 

Comments

Popular posts from this blog

mongodb - Struggling to get ordered results from the last retrieved article, given array of elements to search in -

c# - Pausing a storyboard on TabItem mouse over -

c# - Attribute value in root node of xml Linq to XML -