0

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?

YulePale
  • 6,688
  • 16
  • 46
  • 95
  • 1
    i guess, its better to manage it in some caching service more than server side – Saikat Chakrabortty Sep 23 '19 at 07:23
  • Kindly explain, "and in database maintaining this can cause lots of updates". I don't understand. And how would you do it in a caching service? – YulePale Sep 23 '19 at 07:32
  • @SaikatChakrabortty so you are saying that instead of doing it in the database or server I should do it in a caching service? – YulePale Sep 23 '19 at 07:50

0 Answers0