You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

本地使用Mongoose插入文档时遭遇users.insertOne()缓冲超时错误

本地MongoDB环境下Mongoose插入文档超时:users.insertOne() buffering timed out

错误提示

MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms

我的代码

dbLogic.js

const mongoose = require ("mongoose");
const User = require("./Collections/User")

mongoose.connect("mongodb://localhost:27017")        
//console.log("Connected to db: ");    
           
addUser()        
async function addUser() {        
    const user = await  User.create({ name: "Phiri", age: 40 })

    console.log(user);
}

User.js

const mongoose = require ("mongoose");

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

module.exports = mongoose.model("User", userSchema);

已尝试的操作

  • 更换过连接字符串为mongodb://localhost/myDatabase
  • 当前使用的mongodb://localhost:27017来自MongoDB Compass,且能通过mongosh正常连接数据库、插入记录

完整错误输出

PS C:\Users\my_is\Programming\Project> node dbLogic.js
Listening on Port: 2000
(node:14400) [MONGOOSE] DeprecationWarning: Mongoose: the `strictQuery` option will be switched back to `false` by default in Mongoose 7. Use `mongoose.set('strictQuery', false);` if you want to prepare for this change. Or use `mongoose.set('strictQuery', true);` to suppress this warning.
(Use `node --trace-deprecation ...` to show where the warning was created)
C:\Users\my_is\Programming\Project\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:175
          const err = new MongooseError(message);
                      ^

MongooseError: Operation `users.insertOne()` buffering timed out after 10000ms
    at Timeout.<anonymous> (C:\Users\my_is\Programming\Project\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:175:23)
    at listOnTimeout (node:internal/timers:564:17)
    at process.processTimers (node:internal/timers:507:7)

解决方法

这个超时问题核心是Mongoose未完成数据库连接就执行了插入操作,按以下步骤修复:

  1. 等待连接成功后再执行业务逻辑
    修改dbLogic.js,通过async/await等待连接完成,确保数据库就绪后再创建文档:

    const mongoose = require("mongoose");
    const User = require("./Collections/User");
    
    // 消除strictQuery警告,适配Mongoose版本
    mongoose.set('strictQuery', false);
    
    async function connectAndAddUser() {
      try {
        // 明确指定数据库名,避免Mongoose无法定位目标库
        await mongoose.connect("mongodb://localhost:27017/myDatabase");
        console.log("数据库连接成功");
    
        // 连接完成后执行插入
        const user = await User.create({ name: "Phiri", age: 40 });
        console.log(user);
      } catch (err) {
        console.error("操作失败:", err);
      } finally {
        // 可选:操作完成后关闭连接
        await mongoose.disconnect();
      }
    }
    
    connectAndAddUser();
    
  2. 明确指定数据库名
    连接字符串中必须包含目标数据库名称(比如上述代码中的myDatabase),否则Mongoose无法确定操作的数据库,会导致缓冲超时。

  3. 处理版本警告
    代码开头添加mongoose.set('strictQuery', false);,既消除DeprecationWarning,也适配Mongoose 7及后续版本的默认配置。


内容的提问来源于stack exchange,提问作者user1960836

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 23:00:26