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

Node.js "Not saving but returning success on Ubuntu

Hello! I am having some trouble inserting a document into my MongoDB database from an Ubuntu server.

The strange this is that I am getting the “ok: 1” success response on the server, but when I go check the database I don’t see any data…

Here is my node.js code that saves the document in mongo:

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

    const save = (documentToSave) => {

    return new Promise(resolve => {

        MongoClient.connect(process.env.MONGO_URI, (err, db) => {

            if (err)
                throw new Error(err)

            var dbo = db.db(process.env.DATABASE_NAME)

            console.log('connecting to MongoDB at: ', process.env.MONGO_URI, ', process.env.DATABASE_NAME)

          dbo.collection(process.env.SECTORS_SCRAPER_COLLECTION).insertOne(documentToSave,
                (err, res) => {
                    if (err) throw err
                    db.close()
                    resolve(res.result)
                })

        })

    })

    }

When running this code locally on my mac it resolves with this response:

     {
      n: 1,
      opTime: {
        ts: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1587752219 },
        t: 52
      },
      electionId: 7fffffff0000000000000034,
      ok: 1,
      '$clusterTime': {
        clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1587752219 },
        signature: { hash: [Binary], keyId: [Long] }
      },
      operationTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1587752219 }
    }

When when I refresh the collection in robo3TI can see the new document.

However, when I try to run the same code and environment variables on Ubuntu 18.04.3 it gives this same success result:

     {
      n: 1,
      opTime: {
        ts: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1587753430 },
        t: 52
      },
      electionId: 7fffffff0000000000000034,
      ok: 1,
      '$clusterTime': {
        clusterTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1587753430 },
        signature: { hash: [Binary], keyId: [Long] }
      },
      operationTime: Timestamp { _bsontype: 'Timestamp', low_: 1, high_: 1587753430 }
    }

When I check the database after running this, I don’t see any new data. :cry:

I am confused why I am not getting an error if it isn’t working and would appreciate help if anyone knows what I can do to get it working… I did enable “ufw” firewall on ubuntu so possibly that is part of the issue? :thinking:

Thanks!