I am building backend with MEAN stack, but when I try to update document in the db i am getting an error:
topUp = function(name, amount, callback) {
User.updateOne(
{ "name" : name },
{ $set: { "wallet": amount } },
function(err, results) {
console.log(results);
callback();
});
};
TypeError: User.updateOne is not a function
But e.g. findOne() works fine:
User.findOne({
name: decoded.name
}, function(err, user) {
if (err) throw err;
i
f (!user) {
return res.status(403).send({success: false, msg: 'Authentication failed. User not found.'});
} else {
//res.json({success: true, info: {wallet: user.wallet, userPic: user.userPic}});
topUp(decoded.name, amount, function() {
User.close();
});
}
});
"User" is a Mongo model file.
I am building backend with MEAN stack, but when I try to update document in the db i am getting an error:
topUp = function(name, amount, callback) {
User.updateOne(
{ "name" : name },
{ $set: { "wallet": amount } },
function(err, results) {
console.log(results);
callback();
});
};
TypeError: User.updateOne is not a function
But e.g. findOne() works fine:
User.findOne({
name: decoded.name
}, function(err, user) {
if (err) throw err;
i
f (!user) {
return res.status(403).send({success: false, msg: 'Authentication failed. User not found.'});
} else {
//res.json({success: true, info: {wallet: user.wallet, userPic: user.userPic}});
topUp(decoded.name, amount, function() {
User.close();
});
}
});
"User" is a Mongo model file.
Share Improve this question asked Jul 19, 2016 at 13:44 boooniboooni 1531 gold badge3 silver badges10 bronze badges 6-
because
findOne
is a predefined function butupdateOne()
is not. It should by default update only one record. You can usemulti: true
to update multiple records. – Mohit Bhardwaj Commented Jul 19, 2016 at 13:46 - @MohitBhardwaj well, according to Mongo docs updateOne() is predefined too: proof – boooni Commented Jul 19, 2016 at 13:49
-
I think it's not defined in the database driver that you might be using. I think you are using Mongoose and
updateOne()
is not available there. You cannot use all native mongodb functions with all drivers. – Mohit Bhardwaj Commented Jul 19, 2016 at 13:59 - I am not very sure about it, but that is so as per my understanding. – Mohit Bhardwaj Commented Jul 19, 2016 at 13:59
- 1 @MohitBhardwaj wow, thanks a lot, that's true! I should have used update() which is supported by Mongoose instead of updateOne(). If you write it as an answer I will be glad to accept it:) – boooni Commented Jul 19, 2016 at 14:25
2 Answers
Reset to default 4I think it's not defined in the database driver that you might be using. I think you are using Mongoose and updateOne()
is not available there. You cannot use all native mongodb functions with all drivers
There is an en existing enhancement request for this https://github./Automattic/mongoose/issues/3997 , but maybe the findByIdAndUpdate() method could be a close alternative.