How to remove a value from a field in mongoDB discord.js v13

Viewed 24

I am making an auto-role system for my bot and I have a question about How can I delete a string from the schema.

Here is the code of the model:

//Guild.js
const mongoose = require('mongoose');
const guildConfigSchema = mongoose.Schema({
  guildId: { type: String, match: /\d{18}/igm, required: true },
  autoRoleDisabled: {
    type: Boolean,
  },
  autoRole1: {
    type: String
  },
  autoRole2: {
    type: String
  },
  autoRole3: {
    type: String
  },
  autoRole4: {
    type: String
  },
  autoRole5: {
    type: String
  },
});
module.exports = mongoose.model('guild', guildConfigSchema);

I want to make a remove-autorole command that removes the specified role from the schema but I don't know how.

Example of what I want to do:

        const Welcome = require('../../models/Guild');
        const enteredContent = args[0].toLowerCase();
        if (!enteredContent) return message.channel.send('Please Specify a role! example: role2')
        Welcome.findOne({ guildId: message.guild.id }, async (err, data) => {
            if (data) {
                if (data.autoRoleDisabled == false) {
                    let role;
                    if (enteredContent == 'role1') {
                        role = data.autoRole1
                        data.unset = { role: 1 }
                    }
                    if (enteredContent == 'role2') {
                        role = data.autoRole2
                        data.unset = { role: 1 }
                    }
                    if (enteredContent == 'role3') {
                        role = data.autoRole3
                        data.unset = { role: 1 }
                    }
                    if (enteredContent == 'role4') {
                        role = data.autoRole4
                        data.unset = { role: 1 }
                    }
                    if (enteredContent == 'role5') {
                        role = data.autoRole5
                        data.unset = { role: 1 }
                    }
                    if (!enteredContent == 'role1' || !enteredContent == 'role2' || !enteredContent == 'role3' || !enteredContent == 'role4' || !enteredContent == 'role5') { }
                    try {
                        role
                    } catch (e) {
                        return message.channel.send('There was an error adding this role to auto-role system. Please try again.');
                    }
                } else return message.reply({ content: `<:no:990536144804524084> Auto-role is diabled!` })
                data.save();
            } else {
                return message.channel.send('Your auto-role is not setup');
            }
        })

But data.unset is not working So the question is, How to remove a string(value) from the schema(DB)?

Notes: I am using discord.js v13.8.0 and node.js v16.

0 Answers
Related