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
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.
Here is an image when you go inside of a 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.
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.