Here's an example for all CRUD operations (Create, Read, Update, Delete) using the provided code:
app.post("/nieuwe-bezoeker", bezoekerSchema, async (req, res) => {
// SLA BEZOEKER OP IN DB
const postData = req.body
const logBezoeker = await db.collection("bezoekers").insertOne(postData);
const bericht = `Bezoeker ${postData.voornaam} ${postData.achternaam} is gelogd.`
res.render("nieuwe-bezoeker", { bericht })
})
app.get("/bezoekers", async (req, res) => {
const query = {} // leeg laten om alle documenten op te halen
const options = { sort: { naam: 1 } }
// sorteer op naam oplopend (aflopend is -1)
const gelogdeBezoekers = await db
.collection("bezoekers")
.find(query, options)
.toArray(); // maak array ipv default cursor terug te geven
res.render("bezoekers", { bezoekers: gelogdeBezoekers })
})
app.put("/bezoeker/:id", async (req, res) => {
const bezoekerId = req.params.id
const updatedData = req.body
await db.collection("bezoekers").updateOne({ _id: bezoekerId }, { $set: updatedData })
res.send("Bezoeker bijgewerkt!");
})
app.delete("/bezoeker/:id", async (req, res) => {
const bezoekerId = req.params.id;
await db.collection("bezoekers").deleteOne({ _id: bezoekerId });
res.send("Bezoeker verwijderd!");
});
These examples demonstrate how to perform CRUD operations using Node.js and MongoDB, assuming you have set up appropriate routes and middleware for handling requests.
In the context of MongoDB, "put", "patch", and "update" are terms commonly used in the context of CRUD (Create, Read, Update, Delete) operations, but their exact meanings can vary depending on the specific context or the technology stack being used. Here's a general overview of how these terms might be used in the context of MongoDB:
In summary: