S
S
squadbrodyaga2020-11-17 13:58:37
Node.js
squadbrodyaga, 2020-11-17 13:58:37

[Mongoose] How to change a single field?

Hello.
How to change user data? For example, the database has the following room:

_id: *ид комнаты*
creator:
    _id: *ид создателя*
    login: "Вася"
    select: 
player2:
    _id: *ид второго игрока*
    login: "Петя"
    select:

And in the select field of player2 , I need to enter new data, I first tried this:
await Room.findByIdAndUpdate(*название комнаты*, {
  player2: {
     select: "пишу сюда новые данные"
  }
}

But then everything in the database of player2 is deleted, except for the select field , it looks like this:
_id: *ид комнаты*
creator:
    _id: *ид создателя*
    login: "Вася"
    select: 
player2:
    select: "тут появляются данные, но как видите другие поля удалились"

Then I googled, there was something about $set , and I wrote this:
await Room.findByIdAndUpdate(*название комнаты*, {
  $set: {
    player2: {
       select: "пишу сюда новые данные"
    }
  }
}

But nothing has changed. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey P, 2020-11-17
@squadbrodyaga

Try

Room.update({_id: room._id, "subDocs._id": user._id}, {$set: { <your_data> });

PS Documentation
The solution is stated in the comments

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question