1
I am using Mongodb to do data persistence in my application, with Nodejs.
I make the call like this:
module.exports = function (app) {
var dbConfig = app.config.db;
return {
changeAccountStatus: function (request, response, next, obj) {
dbConfig.mongoclient.connect(dbConfig.mongourl, function(err, db) {
if (err) {
console.log(err);
db.close();
}
else{
var dbo = db.db(dbConfig.mongodbname);
dbo.collection("driver").updateOne({_id: obj._id}, {$set: {accountstatus: obj.accountstatus}}, function(err, result) {
//Código do handler
});
db.close();
}
});
}
}
}
Sending a JSON via POST:
{
"_id":"5a7234ee869cd6058834acae",
"accountstatus":"active"
}
The field $filter
of updateOne
would be: { _id: '5a7234ee869cd6058834acae' }
And the $update
: { '$set': { accountstatus: 'active' } }
In my view, the code is all right; send an existing ID along with the new accountStatus
I want to change. But I always get the message.
{ n: 0, nModified: 0, ok: 1 }
That nothing’s changed. I don’t know what I’m doing wrong.
I figured it could be something like this, in other places I read about it said I could only use the string, but I’ll test anyway, since I’m not getting anywhere.
– Ingo Guilherme Both Eyng
OK, when you finish, come back here and tell me if it worked =]
– BrTkCa
In fact it worked, but only one addendum in his answer, the function
ObjectId()
does not need thenew
, to use directly after givingvar mongodb = require('mongodb')
thus:mongodb.ObjectId(obj._id)
– Ingo Guilherme Both Eyng