Answer the question
In order to leave comments, you need to log in
How to push an object into the model array only if it is not there?
There is such a scheme:
const chat = new schema({
name: {
type: String,
required: true,
unique: true,
},
users: [{
name: {
type: String,
required: true,
unique: true,
}
}],
messages: [{
user: {
type: String,
required: true,
},
message: {
type: String,
required: true,
},
date: {
type: String,
required: true,
}
}]
}, {
versionKey: false,
collation: 'chat'
});
router.post('/join', async (request, response) => {
models.chat.findOneAndUpdate({
name: request.body.data.chat.name
}, {
$push: {
users: {
name: request.body.data.user.name,
}
}
}, {
new: true,
upsert: true,
}, (error, chat) => {
if (!error){
response.json({
status: true,
body: {
chat
},
});
//console.log('Chat update! chat: ', chat);
} else {
//console.log('error: ', error);
}
});
});
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question