xxxxxxxxxx
if (await AnnouncementModel.exists(query)) {
let existingAnnouncement = await AnnouncementModel.findOne(query).exec()
update(existingAnnouncement, editedAnnouncementPayload);
function update(targetObject, obj) {
Object.keys(obj).forEach(function (key) {
if ("object" === typeof obj[key] && !Array.isArray(obj[key])) {
update(targetObject[key], obj[key]);
} else {
targetObject[key] = obj[key];
}
});
}
existingAnnouncement.updatedDateTime = new Date();
existingAnnouncement.save() //or await xModule.updateOne(query, updatePayload).exec()
logger.debug("Document is updated");
logger.debug("Document is reindexed");
return existingAnnouncement;
} else {
throw new Error(`Announcement with announcementId: ${announcementId}, announcerUserId: ${principalUserId} not found,
might be that you are not the creator/owner of this announcement.`)
}
xxxxxxxxxx
// Update all documents in the `mymodels` collection
await MyModel.updateMany({}, { $set: { name: 'foo' } });
xxxxxxxxxx
// V--- THIS WAS ADDED
Cat.findOneAndUpdate({age: 17}, {$set:{name:"Naomi"}}, {new: true}, (err, doc) => {
if (err) {
console.log("Something wrong when updating data!");
}
console.log(doc);
});
xxxxxxxxxx
const userObjectId = mongoose.Types.ObjectId(userIdString);
await UserModel.updateOne({ _id: userObjectId }, { $set: { isVerifiedEmail: true } }).catch(
error => {
console.log(error);
}
);
console.log('user updated');
xxxxxxxxxx
const res = await Person.updateOne({ name: 'Jean-Luc Picard' }, { ship: 'USS Enterprise' });
res.n; // Number of documents matched
res.nModified; // Number of documents modified
xxxxxxxxxx
model.updateOne({_id:'YOURID'}, {DATA YOU WANT TO UPDATE}, (err, result) => {
if(err) throw err
console.log(err)
})
xxxxxxxxxx
// Update the document using `updateOne()`
await CharacterModel.updateOne({ name: 'Jon Snow' }, {
title: 'King in the North'
});
// Load the document to see the updated value
const doc = await CharacterModel.findOne();
doc.title; // "King in the North"
xxxxxxxxxx
router.patch('/:id', (req, res, next) => {
const id = req.params.id;
Product.findByIdAndUpdate(id, req.body, {
new: true
},
function(err, model) {
if (!err) {
res.status(201).json({
data: model
});
} else {
res.status(500).json({
message: "not found any relative data"
})
}
});
});
xxxxxxxxxx
// Update a user's info, by username
/* We’ll expect JSON in this format
{
Username: String,
(required)
Password: String,
(required)
Email: String,
(required)
Birthday: Date
}*/
app.put('/users/:Username', (req, res) => {
Users.findOneAndUpdate({ Username: req.params.Username }, { $set:
{
Username: req.body.Username,
Password: req.body.Password,
Email: req.body.Email,
Birthday: req.body.Birthday
}
},
{ new: true }, // This line makes sure that the updated document is returned
(err, updatedUser) => {
if(err) {
console.error(err);
res.status(500).send('Error: ' + err);
} else {
res.json(updatedUser);
}
});
});
xxxxxxxxxx
try { db.restaurant.updateOne( { "name" : "Central Perk Cafe" }, { $set: { "violations" : 3 } } );} catch (e) { print(e);}
xxxxxxxxxx
db.students3.updateOne(
{ _id: 3 },
[
{ $set: { average: { $trunc: [ { $avg: "$tests" }, 0 ] }, lastUpdate: "$$NOW" } },
{ $set: { grade: { $switch: {
branches: [
{ case: { $gte: [ "$average", 90 ] }, then: "A" },
{ case: { $gte: [ "$average", 80 ] }, then: "B" },
{ case: { $gte: [ "$average", 70 ] }, then: "C" },
{ case: { $gte: [ "$average", 60 ] }, then: "D" }
],
default: "F"
} } } }
]
)