Welcome π
First of all, if you haven't seen Part 1, you should read it or you may be confused. We're going to be using the same database that we created!
Editing & Saving Data
So, you want to edit data and save it to your database. Well, this is how you do that.
We're going to be changing the server's "key" to a new one! This is just an example but, you can use this for your own bot's functionalities.
Let's say that right now the server's "key" is ExampleLol123
and we want to change it to SuperSecretKey
, this is how!
Start off by finding the server's entry by using the "FindOne" function.
Schema.findOne({ Guild: interaction.guild.id }, async (err, data) => {
});
Now that we've found the server's entry, let's change it's "key" to SuperSecretKey
.
In the code below, we'll check if there is any data for the server then if there is, we'll change it.
if (data) {
data["SpecialKey"] = "SuperSecretKey";
}
Finally, let's save it!
data.save();
Wasn't that easy? βοΈ Let's move on to deleting data!
Deleting Data π
Let's say you want to delete a server's data (eg, if the bot was kicked from the server and you don't want to use up space), this is how!
We'll be using the handy findOneAndDelete
function for this part of the tutorial. Basically, you'll find the entry and delete it all in 1 function! π±
Schema.findOneAndDelete({ Guild: interaction.guild.id }, async (err, data) => {
});
If there's an error, we'll log that in the console π
if (err) {
console.log(`βLooks like there's an error: ${err}`);
}
Otherwise, if there isn't an error and everything is good, we'll make sure to log in the console that the operation was successful! β
else {
console.log(`π That was quick. Data deleted successfully! β
`);
}
Learning More...
You've just learned how to π
- Edit Data
- Delete Data
- and.. save it!
But if you want to learn more, please leave a β€οΈ or a Comment π¬ to motivate me to make more of these tutorials!
Thank you for reading! π
Top comments (0)