D
D
dfhkjhg2020-06-29 15:17:38
JavaScript
dfhkjhg, 2020-06-29 15:17:38

How to update data in mongoose array?

There is an object:

{
    id: 8946,
    e: [
        ...,
        {type: 'model', value: 1, other: {
            price: 1525,
            chtoto: 1,
        }}
    ]
}

//достаю из бд
findOne({id: 8946})

Но мне допустим надо достать из массива e обьект где type === 'model' && value === 1
и изменить данные в обьекте other
Как это сделать?)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
hzzzzl, 2020-06-29
@dfhkjhg

try something like this, but it's not accurate

.findOneAndUpdate(
  { id: 8946, 'e.type': 'model', 'e.value': 1 },
  { 
    $set: {
      'e.$.other': { ...newOtherData }   // новый объект
    }
  }
)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question