Update array of objects in MongoDB with Node?

0

Good evening guys.

I'm developing a simple chatbot for testing purposes, but I came across 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 make a put, it overwrites the previous message, as if it were not an array!

Would anyone know why?

    
asked by anonymous 26.02.2018 / 23:46

1 answer

1

By its last image, I think that could solve:

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

In arrayLists use $push instead $set .

    
27.02.2018 / 02:11