Home > Software design >  MongoDB Updating Boolean Inside Object
MongoDB Updating Boolean Inside Object

Time:05-14

I am trying to update a boolean inside of an object in my discord.js v13 bot but it does not update here are the things i've tried:

                    await guildSchema.findOneAndUpdate({
                        logging.enabled: true
                    })
                    await guildSchema.findOneAndUpdate({
                        logging.enabled = true
                    })

But none of these seem to update it here is a screenshot of the db

I also tried:

                    await guildSchema.findOneAndUpdate({
                        logging: {"enabled": true}
                    })

And that updates it but also erases everything else

CodePudding user response:

Your error is passing the update statement to the filter parameter.
Your query will find the first doc with { 'logging.enabled' : true }.
And don't update anything

const filter = { 'logging.enabled' : false};

const update = { 'logging.enabled' : true };

// `doc` is the document _before_ `update` was applied
let doc = await guildSchema.findOneAndUpdate(filter, update);
// doc.logging.enabled is false


// `doc` is the document _after_ `update` was applied because of
// `new: true`
let doc = await guildSchema.findOneAndUpdate(filter, update, {
  new: true
});
// doc.logging.enabled is true
  • Related