Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

377
Visualizações
Using mongoDB with node.js with the driver

I'm trying to use mongoDB with the driver

  1. I installed mongoDB in windows using this guide https://medium.com/@LondonAppBrewery/how-to-download-install-mongodb-on-windows-4ee4b3493514.

  2. I created a new directory and an app.js. (following this guide https://docs.mongodb.com/drivers/node/master/fundamentals/connection/) In the app.js I pasted this:

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. In the terminal I opened mongod in the first shell and in the second shell I used the comand "node app.js" inside its directory.
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

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

don't use MongoDB URI in SRV format as it requires a hostname and a valid domain name. in your case, you are connecting with localhost which does haven't any domain name. instead of use

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);

For more information check this documentation:

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda