Exercise 9 - Node Application Working MongoDB - P2
Exercise 9 - Node Application Working MongoDB - P2
Create a new file named operations.js that contains a few MongoDB operations and add the
following code:
1
Using the Node Module for Database Operations
...
const dboper = require('./operations');
...
dboper.insertDocument(db, { name: "Vadonut", description: "Test"},
"dishes", (result) => {
console.log("Insert Document:\n", result.ops);
dboper.findDocuments(db, "dishes", (docs) => {
console.log("Found Documents:\n", docs);
dboper.updateDocument(db, { name: "Vadonut" },
{ description: "Updated Test" }, "dishes",
(result) => {
console.log("Updated Document:\n", result.result);
dboper.findDocuments(db, "dishes", (docs) => {
console.log("Found Updated Documents:\n", docs);
db.dropCollection("dishes", (result) => {
console.log("Dropped Collection: ", result);
client.close();
});
});
});
});
});
...
Run the server by typing the following at the prompt and observe the results:
npm start