Definition:
db.collection.findOneAndUpdate(filter, update, options)
The
findOneAndUpdate() method has the following form:
db.collection.findOneAndUpdate(
<filter>,
<update document or aggregation pipeline>,
{
projection: <document>,
sort: <document>,
maxTimeMS: <number>,
upsert: <boolean>,
returnNewDocument: <boolean>,
collation: <document>,
arrayFilters: [ <filterdocument1>, ... ]
}
)
As
an Example,
update = async (req, res) => {
let id = req.body._id;
let update = req.body;
if (!req.body) {
return res.status(400).json({
success: false,
error: 'You must provide a body to update
records.',
})
}
Blog.findByIdAndUpdate(id, {$set:update}, function(err, result){
if(err){
console.log(err);
return res.status(404).json({
error,
message: 'Blog not updated.',
})
}
return res.status(200).json({
success: true,
id: Blog._id,
message: 'Blog updated successfully.',
})
});
}
Result look like this -
1.
{data: {…}, status:
200, statusText:
"OK", headers:
{…}, config: {…}, …}
1.
config: {url: "http://localhost:3005/api/Blog/5dd39979acd5e82e103af5df", method: "put", data: "{"name":"Anil","rating":"100","_id":"5dd39979acd5e82e103af5df"}", headers: {…}, transformRequest: Array(1), …}
2.
data: {success: true, message: "Blog updated successfully."}
3.
headers: {content-length: "54", content-type: "application/json; charset=utf-8"}
4.
request: XMLHttpRequest {readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, onreadystatechange: Æ’, …}
5.
status: 200
6.
statusText: "OK"
7.
__proto__: Object