The updateOne function is not working, it returns OK but does not change anything

1

I'm 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 $update : { '$set': { accountstatus: 'active' } }

In my opinion, the code is all right; I 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 }

Nothing has been modified. I do not know what I'm doing wrong.

    
asked by anonymous 08.02.2018 / 18:48

1 answer

2

It may not be finding the document because it searches for _id ObjectID, so you can do the following:

const {ObjectId} = require('mongodb');
[...]
updateOne({_id: new ObjectId(obj._id)}
    
08.02.2018 / 20:07