deno与mongodb的连接失败
我正在尝试将我的deno应用程序连接到mongodb,但出现错误deno与mongodb的连接失败,mongodb,deno,Mongodb,Deno,我正在尝试将我的deno应用程序连接到mongodb,但出现错误 import {MongoClient} from "https://deno.land/x/mongo@v0.21.2/mod.ts"; const client = await new MongoClient(); await client.connect("mongodb+srv://deno:i4vY8AtCEhr6ReqB@sample.7jp1l.mongodb.net/deno?ret
import {MongoClient} from "https://deno.land/x/mongo@v0.21.2/mod.ts";
const client = await new MongoClient();
await client.connect("mongodb+srv://deno:i4vY8AtCEhr6ReqB@sample.7jp1l.mongodb.net/deno?retryWrites=true&w=majority");
const db = client.database("notes");
export default db;
一切似乎都很好,但当我运行应用程序时,我得到了这个错误
error: Uncaught (in promise) Error: MongoError: "Connection failed: failed to lookup address information: nodename nor servname provided, or not known"
throw new MongoError(`Connection failed: ${e.message || e}`);
^
at MongoClient.connect (client.ts:93:15)
at async mongodb.ts:4:1
我看到的两个问题:
- 上面的代码片段仅适用于安装在本地计算机中的Mongo
- 连接字符串使用DNS种子列表,但当前库无法解析为主机列表
const client = new MongoClient();
const db = await client.connect({
db: '<your db or collection with work with>',
tls: true,
servers: [
{
host: '<correct host - the way to get the host - see bellow>',
port: 27017,
},
],
credential: {
username: '<your username>',
password: '<your password>',
mechanism: 'SCRAM-SHA-1',
},
});
const client=new MongoClient();
const db=wait client.connect({
db:“”,
tls:没错,
服务器:[
{
主机:“”,
港口:27017,
},
],
凭证:{
用户名:“”,
密码:“”,
机构:“紧急停堆-SHA-1”,
},
});
如何获得正确的主机:
- 在Mongo Atlas中打开集群
- 选择连接按钮
- 选择连接到应用程序选项
- 选择驱动程序:Node.js和版本:2.2.12或更高版本
- 在这里,您将看到主机跟随字符的列表