I'm trying to do a query to add an element to an array in a document in a mongodb collection, only if this element passes a certain condition.
{
_id: ObjectId,
author: String,
answers: [
{ user: 'user1', answer: 'hello1' },
{ user: 'user2', answer: 'hello2' },
{ user: 'user3', answer: 'hello3' },
]
}
Now, passing an object like this: { user: 'user1', answer: 'hello world' } to my endpoint, I would like the database to check if the user who answered had already answered and if he already had, return an error or something like in MySQL (updatedRows: n) and instead, if the user hadn't already answered, simply push his answer at the bottom of the array of answers
Thanks in advance
userandanswerto apply to more than one author?