首页 > 解决方案 > 无法连接到 mongoDB 图集

问题描述

我已经正确安装了所有东西,但不知道发生了什么。这是代码

// Connect to MongoDB
const dbURI = 'mongodb+srv://<username>:<password>@blogging-site.mantx.mongodb.net/Blogging-site?retryWrites=true&w=majority';
try {
  // Connect to the MongoDB cluster
   mongoose.connect(dbURI,{ useNewUrlParser: true, useUnifiedTopology: true },
    () => console.log(" Mongoose is connected")
  );
} catch (e) {
  console.log("could not connect");
}

我的 VS 代码中的错误是 - 错误:QueryReqWrap.onresolve
处的 queryTxt ETIMEOUT blogging-site.mantx.mongodb.net [as oncomplete] (dns.js:203:19) { errno: undefined, code: 'ETIMEOUT',系统调用:'queryTxt',主机名:'blogging-site.mantx.mongodb.net'} 我正在关注网络忍者教程,希望我没有按照他的指示离开任何步骤。我之前使用过 mongoDB atlas 并在我的计算机上安装了 mongodb。这会产生任何问题吗?

标签: mongodbmongoosemongodb-querymongodb-atlas

解决方案


没有问题,因为“我可以连接”到它。

我可以连接到它

我,一个你不认识的人,一个很可能在世界另一端的人

可能是一个怀有恶意的人(尽管我不是)。

这可能是连接问题(由于互联网不好或其他原因),但更大的问题是

我可以连接到它

因此,也许您希望在将 MongoDB URI 部分公开以供所有人查看之前将其保留,除非您希望邪恶的人从您的数据库中窃取用户信息、会话数据以及其他内容。

快乐学习!!!

评论后编辑 以下是开始使用猫鼬时应该学习的内容:

  -setting up initial(also called default) connection with mongoose.connect()
  -configuring schemas with mongoose.Schema()
  -compiling models with schemas with mongoose.model()
  -saving the doc with mongoose.save()
  -querying the database(findOne, find, deleteOne, updateOne, findById are the 
   ones I find most used)

也许当你习惯了所有这些时,你可以开始学习填充(你可能会在更大的项目中需要它)、创建多个连接和导出模式模式、验证、mongooseJs 的中间件等等,我确信你会自己调查的。

至于“你能告诉我你是如何连接到地图集的吗?”

我刚刚复制了您的代码并在我的计算机上运行。就那么简单。dbURI 变量中的字符串是连接到数据库图集的一种方式。你把它给了我,我使用它并连接到它。


推荐阅读