Why is this mongodb function so slow?

I’m constantly getting 504 Gateway Timeout errors on my server, but only for this mongodb operation. I’ve rewritten it 1000 times and I get the same result. I have an index on the users collection so I know it’s not slow when searching for the user.

mongodb.db.collection("users").findOne({ id: id }, (err, user) => {
        if (err) console.error(err);

        // if it does, return it
        if (user) {
          const simplified = db.simplifyUser(user);
          res.header("Content-type", "text/html");
          return res.end(JSON.stringify(simplified));
        }
        // otherwise create a new user document
        const newUser = createUserObject(id, token, name, email);

        mongodb.db.collection("users").insertOne(newUser);

        const simplified = db.simplifyUser(newUser);
        res.header("Content-type", "text/html");
        return res.end(JSON.stringify(simplified));
      });

Code for connecting to mongodb

if (process.env.NODE_ENV === "production")
    ca = __dirname + "/ssl/mongodb.crt";

const mongoClientOptions = process.env.NODE_ENV === "production" ? { 
    useUnifiedTopology: true,   
    sslCA: ca,
    sslValidate: false,
} : {
    useUnifiedTopology: true,
}

MongoClient.connect (__PATH__,
    mongoClientOptions,
    function (error, client) {

        if (error) return console.error (error);

        // Connect to the database
        const db = client.db (__DATABASE__.__NAME__);
        exports.db = db;
    }
)