Using monk:
var doc =
yield new Promise(function (resolve, reject) {
tokens.findAndModify({
query: {
token: myTokenVar
},
remove: true,
new: false
}, function (err, res) {
if (err)
throw err;
resolve(res);
});
});
The following code above removes every field from the given document but however leaves the document with only the _id field left. It does not completely remove the document.
According the findAndModify source code, the opts object must be provided as a separate parameter. Please try it with the following codes
tokens.findAndModify(
{ query: {
token: myTokenVar
}},
{remove: true, 'new': false},
function (err, res) {
if (err)
console.log(err);
else
console.log(res);
});
Related
I'm trying to return the updated document from mongodb but instead I'm console logging Null, any ideas why this might be happening? It's updating the document in the database, but it doesn't seem to want to return the full document to me so I can console log it.
User.findOneAndUpdate({userEmail}, {$set: {resetPasswordToken: token, resetPasswordExpires: now}}, function (err, res){
console.log(res);
})
Use the {new: true} option.
User.findOneAndUpdate({
email: userEmail
}, {
$set: {
resetPasswordToken: token,
resetPasswordExpires: now
}
}, {
new: true
}, function(err, res) {
console.log(res);
})
I am using mongoose for mongodb queries.
My update query returns null.
What am I doing wrong?
The query is as follows:
Model.findOneAndUpdate(criteria, updatedDetails, { 'new': true})
Example -
I have a user profile which I need to update and send the updated profile back to frontend.
User.findOneAndUpdate({mobile: "9999999999999"}, { address: "test address" }, {'new': true} )
But the result comes null instead of the updated profile.
findOneAndUpdate is now desprecated
use update, here is sample code
exports.updateSomething = (req, res) => {
Model.update({
_id: req.params.id
}, {
$set: {
bla: req.body.bla
}
})
.then(data => {
return res.status(200).json({
success: true,
message: 'Updated successfully'
})
})
.catch(err => {
return res.status(200).json({
success: false,
message: err.message
})
})
}
I am trying to update a document with nested subdocuments, but i always retrieve the previus document.
i tried
{ returnOriginal: false }
but it is not working...
this is my code in nodejs
almacenCtrl.updateAlmacen = async (req, res) => {
almacen = await almacenModel.findOneAndUpdate(req.params.id, { $set: req.body }, { returnOriginal: false }, function (err, updated) {
res.json(updated)
})
}
what am i doing wrong?
//After update i check with mongoshell and the update was updated successfully
Use {new : true} as given below:
almacenCtrl.updateAlmacen = async (req, res) => {
almacen = await almacenModel.findOneAndUpdate(req.params.id, { $set: req.body }, { new: true }, function (err, updated) {
res.json(updated)
})
}
How to update metadata in mongoDB(Nodejs).
gfs.collection('uploads').updateOne({ filename:image}, {$set:
{metadata.likes:1}},
function(err, res) {
if (err) throw err;
console.log("1 document updated");
});
As your code will work if you add metadata.likes in brackets like
gfs.collection('uploads').updateOne({ filename:image}, {$set:
{'metadata.likes':1}},
function(err, res) {
if (err) throw err;
console.log("1 document updated");
});
In this case "metadata.likes" would be equal to 1. Every time it will update it and set it to 1. If you want to increment "metadata.likes" by 1, so try this
gfs.collection('uploads').updateOne({ filename:image}, {$inc:
{'metadata.likes':1}},
function(err, res) {
if (err) throw err;
console.log("1 document updated");
});
I want to get all stream data with thread value but not including null. On my mongodb console it works with $ne but on my query sails model it always returns undefined?
Example query:
Stream.findOne({thread: {$ne: null } }, function(err, st){
if(err) return err;
console.log("st", st);
});
How can I resolve this?
Use the .native() method:
Stream.native(function(err, collection) {
if (err) return res.serverError(err);
collection.find({
"thread": { "$ne": null }
}).toArray(function(err, st) {
if (err) return res.serverError(err);
console.log("st", st);
return res.ok(st);
});
});
Or the .where() method:
var myQuery = Stream.find();
myQuery.where({'thread':{ '$ne': null}});
myQuery.exec(function callBack(err, results){
console.log(results)
});