'Value not saving in mongodb
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: ",",
}
]
},
});
Solution 1:[1]
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": ",",
}
]
Solution 2:[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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | It'sMateo20 |
Solution 2 |