【问题标题】:Using mongoDB with node.js with the driver将 mongoDB 与 node.js 与驱动程序一起使用
【发布时间】:2021-12-04 10:48:57
【问题描述】:

我正在尝试将 mongoDB 与驱动程序一起使用

  1. 我使用本指南 https://medium.com/@LondonAppBrewery/how-to-download-install-mongodb-on-windows-4ee4b3493514 在 Windows 中安装了 mongoDB。

  2. 我创建了一个新目录和一个 app.js。 (按照本指南https://docs.mongodb.com/drivers/node/master/fundamentals/connection/) 在 app.js 我粘贴了这个:

const { MongoClient } = require("mongodb");

// Connection URI
const uri =
  "mongodb+srv://sample-hostname:27017/?maxPoolSize=20&w=majority";

// Create a new MongoClient
const client = new MongoClient(uri);

async function run() {
  try {
    // Connect the client to the server
    await client.connect();

    // Establish and verify connection
    await client.db("admin").command({ ping: 1 });
    console.log("Connected successfully to server");
  } finally {
    // Ensures that the client will close when you finish/error
    await client.close();
  }
}
run().catch(console.dir);
  1. 在终端中,我在第一个 shell 中打开了 mongod,在第二个 shell 中,我在其目录中使用了命令“node app.js”。
The terminal printed this error:
C:\Users\Feder\Desktop\udemy\FruitProject\node_modules\mongodb-connection-string-url\lib\index.js:105
            throw new MongoParseError('mongodb+srv URI cannot have port number');
            ^

MongoParseError: mongodb+srv URI cannot have port number
    at new ConnectionString (C:\Users\Feder\Desktop\udemy\FruitProject\node_modules\←[4mmongodb-connection-string-url←[24m\lib\index.js:105:19)
    at Object.parseOptions (C:\Users\Feder\Desktop\udemy\FruitProject\node_modules\←[4mmongodb←[24m\lib\connection_string.js:210:17)
    at new MongoClient (C:\Users\Feder\Desktop\udemy\FruitProject\node_modules\←[4mmongodb←[24m\lib\mongo_client.js:62:46)
    at Object.<anonymous> (C:\Users\Feder\Desktop\udemy\FruitProject\app.js:8:16)
←[90m    at Module._compile (internal/modules/cjs/loader.js:1072:14)←[39m
←[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)←[39m
←[90m    at Module.load (internal/modules/cjs/loader.js:937:32)←[39m
←[90m    at Function.Module._load (internal/modules/cjs/loader.js:778:12)←[39m
←[90m    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)←[39m
←[90m    at internal/main/run_main_module.js:17:47←[39m

【问题讨论】:

    标签: javascript node.js mongodb driver


    【解决方案1】:

    不要使用 SRV 格式的 MongoDB URI,因为它需要主机名和有效域名。 在您的情况下,您正在连接没有任何域名的 localhost 。 而不是使用

    const { MongoClient } = require("mongodb");
    
    // Connection URI
    const uri =
      "mongodb://sample-hostname:27017/?maxPoolSize=20&w=majority";
    
    // Create a new MongoClient
    const client = new MongoClient(uri);
    
    async function run() {
      try {
        // Connect the client to the server
        const connection = await client.connect();
        if(connection){
           console.log("server connected successfully");
        }
    
        // Establish and verify connection
        // await client.db("admin").command({ ping: 1 });
        // console.log("Connected successfully to server");
      } finally {
        // Ensures that the client will close when you finish/error
        await client.close();
      }
    }
    run().catch(console.dir);
    

    有关更多信息,请查看此文档:

    https://github.com/mongodb/specifications/blob/master/source/initial-dns-seedlist-discovery/initial-dns-seedlist-discovery.rst#seedlist-discovery

    【讨论】:

    • 我应该使用您在 app.js 中发布的代码吗?它不打印 console.log 连接成功
    • 你可以写类似 const connection =await client.connect(); if(connection){ console.log("连接成功") }
    猜你喜欢
    • 1970-01-01
    • 2018-01-13
    • 2015-04-16
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 2017-08-06
    相关资源
    最近更新 更多