MongoDB
How use MongoDB module?
The following properties are requested in order to activate the MongoDB module:
mongodb: {
enabled: true,
uri: "mongodb+srv://<user>:<password>@<host>",
name: "powerdiscord" // Name of your database
}
Once the module is activated, you can start using the database property inside the client. For example:
let db = await client.database(query, collection);
If you do a console.log(db) you'll get something like this:
db: {
propiertyExample: "data saved",
propiertyExample2: "Hello!",
save(),
reload();
}
When you execute the database function, the client searches for a document that matches the entered query within the collection. If one doesn't exist, it will create one in memory so you can edit it from code. When you use db.save()
, the document will be created with the specified information in the database. The system was designed this way to avoid generating unnecessary documents.
If you want to enter, modify, or delete any data within the database, you can do so in this way:
// Write data
db.propiertyExample = "new data";
await db.save();
// Delete data
delete db.propiertyExample;
await db.save();
// Read data
const pe = db.propiertyExample;
console.log(pe);
Last updated