skip to Main Content

MongoDB and Mongoose Error: Operation users.insertOne() Buffering Timed Out

Description:

I’m new to MongoDB and Mongoose, and I’ve encountered an error that I can’t seem to resolve. Here’s the error message I’m getting:

[const err = new MongooseError(message);
          ^
MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms
    at Timeout.<anonymous> (F:WorkInformationWEB DEVELOPMENTNew foldermongoosenode_modulesmongooselibdriversnode-mongodb-nativecollection.js:186:23)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7)

Node.js v18.18.0]

Code:

  1. script.js:
import User from './User.js';
import mongoose from "mongoose"

mongoose.connect("mongodb://localhost:27017/testDB", {
  useNewUrlParser: true,
});

run();
async function run() {
    const user = new User({ name: "John", age: 26 });
    await user.save(); // You should call save on the user object, not the User model
    console.log(user);
}
  1. User.js:
import mongoose from "mongoose";

const userSchema = new mongoose.Schema({
    name: String,
    age: Number,
});

const User = mongoose.model("User", userSchema);

export default User;

Problem:

I’m trying to learn MongoDB and Mongoose, and my goal is to create a simple user document and save it to the database. However, I’m encountering this error, and I’m not sure how to resolve it.

What I’ve Tried:

  • Checked the MongoDB server is running.
  • Verified that the connection URL in mongoose.connect matches my MongoDB server’s address and port.
  • Disabled IPv6 in the connection settings.
  • Confirmed there are no firewall rules blocking the connection.

Additional Information:

  • Node.js version: 18.18.0
  • MongoDB version: [Your MongoDB version]

I’d appreciate any guidance on resolving this issue and understanding what might be causing the timeout error. Thank you for your help!

2

Answers


  1. Since you are just starting out using MongoDB with mongoose why don’t you try creating yourself a basic connection module and importing it before you execute your run() function. That way you can ensure the asynchronous call by mongoose.connect has been completed . A really simple example would be:

    dbConnect.js

    const dbConnect = async () => {
      try {
        // await the connection
        const con = await mongoose.connect('mongodb://localhost:27017/testDB');
        console.log(`Connected to mongodb: ${con.connection.host}`);
      } catch (error) {
        console.error(error.message);
      }
    }
    
    export default dbConnect;
    

    Then in your script.js:

    import User from './User.js';
    import dbConnect from './dbConnect.js'; //<Import the function
    import mongoose from "mongoose"
    
    dbConnect(); //< Execute the function
    
    run();
    async function run() {
        const user = new User({ name: "John", age: 26 });
        await user.save(); // You should call save on the user object, not the User model
        console.log(user);
    }
    

    When you start to use a server like express and handle requests you will need to modify your approach but hopefully this gets you connected to the database and you can experiment with creating and retrieving documents.

    Login or Signup to reply.
  2. The only thing you have to do to wait for the connection

    mongoose.connect("mongodb://localhost:27017/testDB", {
      useNewUrlParser: true,
    }).then(() => {
      run();
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search