Query ` buffering timed out after 10000ms

Hello,

I am using Nodejs Application with MongoDb Atlas and facing a critical issue with our cron job’s connectivity to MongoDB. Despite multiple troubleshooting attempts, the cron job consistently times out when querying the database.

Error message : “Error decrementing job post durations: MongooseError: Operation employerprofiles.find() buffering timed out after 10000ms”

const cron = require(‘node-cron’);
const EmployerProfile = require(‘…/models/employers’);

// Define a cron job function to decrement jobPostDuration for each employer
const decrementJobPostDuration = async () => {
try {
// Find all employers with a non-zero jobPostDuration
console.log(‘Before query execution’);
const employers = await EmployerProfile.find({ ‘subscription.jobPostDuration’: { $gt: 0 } }).maxTimeMS(60000); // 60 seconds
console.log(employers);
console.log(‘After query execution’);

    // Decrement jobPostDuration for each employer
    await Promise.all(employers.map(async (employer) => {
        if (employer.subscription.jobPostDuration > 0) {
            employer.subscription.jobPostDuration--;
            await employer.save();
        }
    }));

    console.log('Job post durations decremented successfully.');
} catch (error) {
    console.error('Error decrementing job post durations:', error);
}

};

// Schedule the cron job to run every day at midnight (00:00) in Canada time zone
cron.schedule(‘* * * * *’, async () => {
console.log(‘Running job post duration cron job…’);
await decrementJobPostDuration();
}, {
timezone: ‘America/Toronto’
});

If anyone knows how to resolve this connectivity problem please let me know.

Thank you!

Have you tried running the find query outside of mongoose, straight on the DB?

Can you run any query against that server without getting a timeout?

Do you just get this issue when running through the node-cron wrapper?