5

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. 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

Community
  • 1
  • 1
  • I don't know the answer, but at least I want to say thank you for posting a complete, detailed, well-written and clear question, with relevant code and debugging attempt. These are WAY too rare. – Jeremy Thille Apr 09 '20 at 14:48
  • 1
    @Rajitha : In general when you get a connection string provided by `MongoDB Atlas` it usually be like `"mongodb+srv://userName:password@cluster0-abcde.mongodb.net/test?retryWrites=true&w=majority";` So in the string `test` is `test` DB on Atlas cluster, Did you change it to your actual DB name ? I guess you might have done it but usually this can be a case otherwise you should be seeing collections atleast.. – whoami - fakeFaceTrueSoul Apr 09 '20 at 14:57
  • @whoami @JeremyThille Thanks for looking into the issue . I tried both `test` and `DB name` in the uri but it's the same result I'm getting – Rajitha Warusavitarana Apr 09 '20 at 15:27
  • Just to rule out the simple problems - database and collection names are case-sensitive. For further debugging, try setting `slowms` to 0 with [db.setProfilingLevel(0,0)](https://docs.mongodb.com/manual/reference/method/db.setProfilingLevel/#db-setprofilinglevel), then try your queries again. Then download the mongod log. Every query should be included in the log. – Joe Apr 10 '20 at 02:05
  • @Joe Thanks for the suggestion. Unfortunately shared clusters does not provide downloadable logs in MongoDB Atlas . – Rajitha Warusavitarana Apr 10 '20 at 06:27

1 Answers1

0

The structure of holding databases are different in mlab and mongoDB Atlas. The mlab shared cluster represents one database while the mongoDB atlas shared cluster can contain multiple databases.

The below image shows the mlab databases.

Databases in mlab

Here is an image when you go inside of a database

database

After the migration process (migrated using the tool provided by mlab and Atlas). It created a shared clusted named maturify-demo and a database named maturify_demo. Look at the below images.

Atlas cluster atlas cluster

Database inside the cluster atlas database

During the migration process it changed the cluster name that was used in Mlab (maturify_demo to maturify-demo)

When connecting to the database using the client I used maturify-demo as the Db name thinking that the cluster represents a database as Mlab (cachedDb = client.db('maturify-demo');). Which actually has to be maturify_demo. But when I am testing the database connection using db.serverConfig.isConnected() and db.databaseName. It returned true and maturify-demo which was a bit confusing where it shows a database which is not available in MongoDB Atlas. As @Joe mentioned in the below comment it allows to add documents as a new database even though the database currently does not exist.

  • It doesn't return "database not found" because that is a valid handle, ready for you to begin inserting documents. There was com discussion on [Check if MongoDB database exists](https://stackoverflow.com/questions/7049722/check-if-mongodb-database-exists) – Joe Apr 10 '20 at 19:41
  • @Joe Thank you for the information :). I'll update my answer. – Rajitha Warusavitarana Apr 11 '20 at 07:55