Troubleshooting MongoDB Atlas Connection for Random NBA Player Data in a WordPress Sports Analytics App

Hi MongoDB community,

I’m developing a sports analytics app using MongoDB Atlas for data storage and WordPress as the frontend to display NBA player stats and analytics. I’m generating random NBA player data (e.g., names, points, rebounds) to populate a collection for testing queries and WordPress REST API integration. I’m using the MongoDB Node.js driver to insert this data, as part of a random data generator I’m working on. Below is my code (with Atlas connection string username/password swapped out), the error I’m encountering when running it from my WordPress server, and my environment details.

I can connect to the Atlas cluster from a local Node.js script and other apps (e.g., a Flask backend) with no issues, and there are no firewall or IP allowlist problems. However, when I trigger the script from my WordPress server to populate the database, I’m hitting a connection timeout, possibly due to the PHP-to-Node setup or Atlas configuration.

Code Sample

Here’s my Node.js script to generate and insert random NBA player data:

const { MongoClient } = require(‘mongodb’);

const uri = ‘mongodb+srv://:@cluster0.fme2j91.mongodb.net/?retryWrites=true&w=majority’;
const client = new MongoClient(uri);

async function generateAndInsertPlayers() {
try {
await client.connect();
const db = client.db(‘sportsDB’);
const collection = db.collection(‘players’);

const players = [];
const names = ['LeBron James', 'Stephen Curry', 'Kevin Durant', 'Giannis Antetokounmpo'];
for (let i = 0; i < 1000; i++) {
  players.push({
    name: names[Math.floor(Math.random() * names.length)],
    points: Math.floor(Math.random() * 50),
    rebounds: Math.floor(Math.random() * 20),
    assists: Math.floor(Math.random() * 15),
    createdAt: new Date()
  });
}



await collection.insertMany(players, { ordered: false });
console.log('Inserted 1000 players');

} catch (error) {
console.error(‘Error:’, error);
} finally {
await client.close();
}
}

generateAndInsertPlayers();

esult

Error: MongoServerError: connection to ac-3jxjrla-shard-00-00.fme2j91.mongodb.net:27017 timed out

Environment Details
MongoDB Node.js Driver Version 6.3.0
Node.js Version 18.12.0
MongoDB Atlas Cluster M0 Free Tier
WordPress Version 6.4.3
PHP Version 8.1
libmongoc SSL enabled
libmongoc SSL library OpenSSL
Operating System Ubuntu 22.04

I’m triggering this script from a WordPress plugin using a PHP exec() call to populate the database, which is then queried via the WordPress REST API for the frontend. Small inserts work locally, but the timeout occurs when running from the WordPress server. The random data generator I’m working on shapes the data structure, but I’m stuck on this connection issue here: Random NBA Player Quiz Generator 🏀🎖️ Do you know these roles?🏆

Has anyone integrated MongoDB Atlas with WordPress for similar use cases? Could this be an Atlas free tier limitation, the PHP exec() setup, or a Node.js driver issue? Any debugging tips or suggestions for generating and inserting test data efficiently would be greatly appreciated!

Generate the players (run the for loop) before you connect to your DB, then insert in batches, I think inserting all 1000 at once might be the issue, instead, insert 50 (20 batches) or 100 (10 batches). Hope this helps.

However, when I trigger the script from my WordPress server to populate the database, I’m hitting a connection timeout, possibly due to the PHP-to-Node setup or Atlas configuration.

This sounds very much like the IP Access List is not correctly configured, which is a likely failure reason when the code works fine from one machine, but fails from another.


If you don’t mind, I am a little surprised at your setup. You are running PHP, and from the output you posted it looks like you installed the MongoDB extension (ext-mongodb). All of the access to MongoDB however is done from a node.js script called through exec - why not load data using the PHP driver instead of using exec to call into a node script?