0

In my NodeJS app I have this Update:

//here I am passing as string, but even as new ObjectID(docId) does not work.
let filter = { 'TepDocSignId' : docId, 'ClientId': clientId };
let update = { 'Solved' : true,  SolvedOn : new Date()};


const resp = await db.collection(process.env.MONGODB_WARNING_COLLECTION_NAME)
                        .updateOne({ filter }, { '$set' : update });

On the Node it DOES NOT work, The matchedCount and modifiedCount are allways 0

But If I do the same update on RoboMongo, it works fine!

What is going on?

Is there some kind of profiler that I can see what is doing on the Node environment?

I am using :

"aws-sdk": "^2.590.0",
"mongodb": "^3.5.5",

And the data are:

process.env.MONGODB_WARNING_COLLECTION_NAME = 'Warning'
docId = '5e29197dac26760002f5a7b5'
clientId = '5caf91cd800fc20002cad0fb'

The full code is (here I am using the IDs as ObjectID, but does not matter if I parse or pass as string it does not match anyting...)

const MongoClient = require('mongodb').MongoClient;
const ObjectID = require('mongodb').ObjectID

    let dbConnString = process.env.MONGODB_CONNECTION_STRING;
    let dbName = process.env.MONGODB_DATABASE_NAME;
    let db;

    const client = new MongoClient(dbConnString, {
        useNewUrlParser: true,
        useUnifiedTopology: true
    });

    const createConn = async () => {
        await client.connect();
        db = client.db(dbName);
    };

async function partnerWarningOff(docId, partnerId) {

        if (!client.isConnected()) {
            try {
                await createConn();
            } catch (e) {
                throw new Error(`partnerWarningOff - OpenningConn Error: ${e}`);
            }
        }

        console.log('process.env.MONGODB_WARNING_COLLECTION_NAME', process.env.MONGODB_WARNING_COLLECTION_NAME);
        console.log('docId', docId);
        console.log('partnerId', partnerId);

        let dId = new ObjectID(docId);
        let pId = new ObjectID(partnerId);

        let filter = { 'TepDocSignId' : dId, 'PartnerId': pId };
        let update = { 'Solved' : true,  'SolvedOn' : new Date()};


        const resp = await db.collection(process.env.MONGODB_WARNING_COLLECTION_NAME)
                                .updateOne({ filter }, { '$set' : update });

        console.log('resp', resp);

        if (!resp) {
            throw new Error(`partnerWarningOff Error solving tep warning with DocId ${docId}`);
        }

    };

cheers
2Fast4YouBR
  • 1,012
  • 1
  • 12
  • 22

2 Answers2

0

Found the problem.

The handler was passing docId as ObjectID and the ClientId as string.

The only thing I had to do, was change the way the handler was calling the function, the correct way is

myfuncName(doc._id**.toString()**, doc.clientId).

This way I have all in string, the same way is stored in the other entity.

2Fast4YouBR
  • 1,012
  • 1
  • 12
  • 22
0

You can Follow this code..

 const response = await db.collection.findOneAndUpdate({
   TepDocSignId : dId,
   PartnerId: pId 
  }, {
    $set: req.body,
  }, {
    new: true
  })

Shuvro
  • 222
  • 3
  • 7