MongoDB.live, free & fully virtual. June 9th - 10th. Register Now

Issue when migrating from mlab to Atlas

I am migrating a database from Mlab to MongoDB Atlas. We had to upgrade the npm version of mongodb to 3.4.1 as the MongoDB atlas database version is 4.2.5.

The connection function has been updated as said in this [answer][1]. But after upgrading the npm version to 3.4.1 the findOne query returns a null value even-though the document is available in the collection. Here is the code section related of the findOne query,

  db.collection('organisations').findOne({ _id: database.ObjectID(orgState) })
    .then((activeOrganisation) => {
      console.log(activeOrganisation);
      data.activeOrganisation = activeOrganisation;
      callback(null, activeOrganisation);
    }, (error) => {
      callback(error, null);
    });

Because of this I was wondering whether there is a problem with the database connection so I tested it with running db.serverConfig.isConnected() , db.databaseName and db.listCollections().toArray(). The isconnected returned true and the returned database name is also correct. But db.listCollections().toArray() returned an empty array which means there are no collections in my database which cannot be.

Then I tried a findOneAndUpdate query just to check what happens with that. Here is the relevant code for it,

db.collection('users').findOneAndUpdate(
        { emails: { $elemMatch: { email: "rajitha1591@outlook.com" } } },
        { $addToSet: { unsubscribedEmails: "models" } })
        .then((result) => {
          console.log(result);
    
            if (!result) {
                console.error('Error: ', 'User not found')
            }
            console.log('Output: ', 'Sucessfully unsubscribed');
            callback(null,'Successful')
        }, (error) => {
            callback(error, null);
        });

The result contained,

{
  lastErrorObject: { n: 0, updatedExisting: false },
  value: null,
  ok: 1,
  '$clusterTime': {
    clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1586436331 },
    signature: { hash: [Binary], keyId: [Long] }
  },
  operationTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1586436331 }
}

This clearly says that the document didn’t got updated(updatedExisting: false). I checked the related document in MongoDB Atlas using the web browser as well and the document wasn’t updated by adding the "models" value to the unsubscribedEmails array.

In addition to that I tried a fresh install of node_modules by deleting the package-lock.json as well.

Since I migrated the database from mlab is it a possibility that exceeding limits of MongoDB shared cluster to occur this issue.

It would be nice to hear suggestions regarding this issue
[1]: https://stackoverflow.com/questions/47662220/db-collection-is-not-a-function-when-using-mongoclient-v3-0/47974041#47974041