“Como conectar o banco de dados Mongoose com o NodeJS” Respostas de código

Nodejs de conexão Mongoose

const mongoose = require('mongoose');

const connectDB = async () => {
    mongoose
        .connect('mongodb://localhost:27017/playground', {
            useCreateIndex: true,
            useNewUrlParser: true,
            useUnifiedTopology: true,
            useFindAndModify: false
        })
        .then(() => console.log('Connected Successfully'))
        .catch((err) => console.error('Not Connected'));
}

module.exports = connectDB;
Uptight Unicorn

Conectando -se ao MongoDB usando o Mongoose

//Import the mongoose module
var mongoose = require('mongoose');

//Set up default mongoose connection
var mongoDB = 'mongodb://127.0.0.1/my_database';
mongoose.connect(mongoDB, {useNewUrlParser: true, useUnifiedTopology: true});

//Get the default connection
var db = mongoose.connection;

//Bind connection to error event (to get notification of connection errors)
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
TJ Max

Como conectar o banco de dados Mongoose com o NodeJS

const mongoose = require("mongoose");

const databaseConnection = () => {
  const dbUri = process.env.DB_URI || "mongodb://localhost:27017/codershouse";
  mongoose
    .connect(dbUri, {
      useNewUrlParser: true,
      useUnifiedTopology: true,
    })
    .then((data) => {
      console.log(
        `MongoDb Database Connected to the Server : ${data.connection.host}`
      );
    })
    .catch((err) => {
      console.log(`Some Database Connection Error Occured :- ${err}`);
    });
};

module.exports = databaseConnection;
Coding is fun

Node JS Conecte -se ao MongoDB usando o Mongoose


//connect with mongodb
mongoose.connect('mongodb://localhost:27017/your_db_name', {useNewUrlParser: true});
//you can also specify with user and pass
mongoose.connect('mongodb://username:password@host:port/database?options...', {useNewUrlParser: true});
//or goto docs https://mongoosejs.com/docs/connections.html
kepl3r

Respostas semelhantes a “Como conectar o banco de dados Mongoose com o NodeJS”

Perguntas semelhantes a “Como conectar o banco de dados Mongoose com o NodeJS”

Mais respostas relacionadas para “Como conectar o banco de dados Mongoose com o NodeJS” em JavaScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código