Is it possible to set mongodb to automatically update a document when a certain condition is met?
Here is my schema:
const ChatRoomSchema = new mongoose.Schema({
membersOfThisChatRoom: [{
objectIdOfMember: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
},
joinedOn: {
type: Date,
default: Date.now,
required: true,
}
}],
adminsOfThisChatRoom: {
type: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true,
}],
},
});
Is it possible to make it that if the last admin is deleted and there are still users in the chatroom, mongodb/mongoose to automatically set a user(preferably the one who joined the earliest) as an admin?
Or do I have to do this in the server side logic?