skip to Main Content
const serverS = await server.findOne({ guildID: message.guild.id });
serverS.settings[1]["links"] = true;
serverS.save()

The code I use ^^^^

So when it saves the changes I check the database using MongoDB compass I don’t see the change appearing in the array object

schema if needed to resolve problem

const ServerSchema = new mongoose.Schema({
    guildID: { type: String },
    settings: {
        type: Array, default: [
            {
            //Some other stuff
            },
            {
                links: false,
            },
            {
                prefix: ",",
            }
        ]
    },
});

2

Answers


  1. Chosen as BEST ANSWER

    Found a way on how to update a part of a array using javascript and mongoose:

    For example:

    collection name
    ||    array name                   values
    ||    ||          id               ||
    /    /          /               /
    data.settings.set(0, {lang: "english", prefix: "!"})
    data.save()
    

    Saved Data:

    "settings": [
       {
          "lang": "english",
          "prefix": ",",
       }
    ]
    

  2. The save() method returns a promise, so you should await the save as a following:

    const serverS = await server.findOne({ guildID: message.guild.id });
    serverS.settings[1]["links"] = true;
    const dataSaved = await serverS.save(); //if the save fail will throw an error 
    console.log({dataSaved}) //to verify the data was saved 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search