Update object array in Mongodb with Node?

Asked

Viewed 399 times

0

Good evening, everyone.

I’m developing a simple chatbot for testing purposes, but I ran into a problem. I have a user, when starting a conversation, a bot is generated, and this bot has an array of messages and responses. I want every time I send a message, it interprets, responds, and both the message and the response are stored in the same object. However, currently, whenever I want to put it, it overwrites the previous message, as if it were not an array!

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

Would anyone know why?

1 answer

1


From your last image, I think this could solve:

module.exports = async (id, data) => {
  await Bot.findByIdAndUpdate(id, {
    $set: {
      name: data.name,
    },
    $push: {
      messages: data.messages,
    },
  });
}

In arrayLists use $push instead of $set.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.