Home > other >  mongoose update inside array object
mongoose update inside array object

Time:10-06

How to update mongodb based image

need to update in_state Object based on testid and user_id

code

 const test = await usertest.update( 
      { "testid" :"oOEbG3ycsl5ZIPrFNk172SVma0zTwD6xHvpUM4JWKAj9fg18nRCtq1B0XLdYeuiQh","user_id":"63297394d10aa70d52708a4c"},
      {
       $set: {
       
          'testdata.1.in_state': in_state
       } 
      }
     
     )

how can achieve this

CodePudding user response:

Try referencing the testdata elements with $:

const test = await usertest.update(
  {
    testid: 'oOEbG3ycsl5ZIPrFNk172SVma0zTwD6xHvpUM4JWKAj9fg18nRCtq1B0XLdYeuiQh',
    user_id: '63297394d10aa70d52708a4c',
  },
  {
    $set: {
      'testdata.$.in_state': in_state,
    },
  }
);
  • Related