I would like to console.log() the results of a mongodb database query without going into the mongo shell. Below is my code. The relevant parts are in bold.
router.put('/quotes/:id', (req, res, next) => {
let personToUpdate = req.params.id;
**console.log("old name: " + JSON.stringify(db.collection("quotes").findOne({ _id: ObjectId(personToUpdate) })) );**
db.collection("quotes").findOneAndUpdate(
{ _id: ObjectId(personToUpdate)},
{$set: {
name: req.body.name
}
}, function (err, object) {
if (err) {
console.warn(err.message);
} else {
**console.log("new name: " + req.body.name);**
}
}
);
});
I have tried:
console.dir(), but that returns [object, object]. I have tried print() and printjson(), but those are mongo shell exclusive commands.
The end result should be that when the user changes a name through a put request, first it logs the old name that is stored in the database to the console, then at the end of the findOneAndUpdate() mongodb command to change the name, it logs the new name that the user submitted through req.body.name
findOneworks: mongodb.github.io/node-mongodb-native/2.2/api/…