Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

mongoose connect

import mongoose from 'mongoose'

export const connectDb = async () => {
  try {
    await mongoose.connect('mongodb://localhost:27017/test', {
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useCreateIndex: true,
    })
  } catch (error) {
    console.log(error.message)
  }
}
Comment

mongoose connection nodejs

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

connecting to mongoDB using 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:'));
Comment

mongoose connection

const mongoose = require("mongoose");

//  database connection with mongoose
mongoose.connect("mongodb://localhost/todos", {
        useNewUrlParser: true,
        useUnifiedTopology: true
    })
    .then(() => console.log("connection successful"))
    .catch((err) => console.log(err));
Comment

mongoose connect to mongodb

// local conecation

const mongoose = require("mongoose");

mongoose
  .connect("mongodb://localhost:27017/testdb")
  .then(() => console.log("Connected to MongoDB..."))
  .catch((err) => console.error("Could not connect to MongoDB...", err));
Comment

mongoose connect

mongoose.connect('mongodb://username:password@host:port/database?options...', {useNewUrlParser: true});
Comment

create new connection in mongoose

const conn = mongoose.createConnection('mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]', options);
Comment

mongoose connection in express

//config/db.js
const mongoose = require('mongoose');

const connect = async () => {
  try {
    await mongoose.connect(process.env.MONGODB);
    console.log('database connect');
  } catch (error) {
    console.log(error.message);
  }
};

module.exports = connect;

//index.js 
require('dotenv').config();
const connect = require('./config/db');

connect();

Comment

mongoose connect

// getting-started.js
const mongoose = require('mongoose');

main().catch(err => console.log(err));

async function main() {
  await mongoose.connect('mongodb://localhost:27017/test');
  
  // use `await mongoose.connect('mongodb://user:password@localhost:27017/test');` if your database has auth enabled
}
Comment

how to connect mongoose

const mongoose = require('mongoose');

const uri = process.env.MONGO_URI || 'mongodb://localhost/test';

mongoose.connect(uri, function(err, res) {
  ...
});
Comment

PREVIOUS NEXT
Code Example
Javascript :: remove whitespace javascript 
Javascript :: how to get day from javascript date 
Javascript :: open google chrome in puppeteer macos 
Javascript :: get json from url c# 
Javascript :: jquery trigger click other element 
Javascript :: js go back 
Javascript :: Uncaught ReferenceError: axios is not defined 
Javascript :: remove duplicate object from array 
Javascript :: new date with date + 1 
Javascript :: js sort by name 
Javascript :: veu scroll to top 
Javascript :: mongoose.connect localhost 
Javascript :: document.queryselector vs document.getelementbyid 
Javascript :: .style.display 
Javascript :: js scroll page horizontally with mouse wheel 
Javascript :: nestjs cors origin 
Javascript :: get value of checked radio button jquery 
Javascript :: backbone events 
Javascript :: nuxt window is not defined 
Javascript :: vue js store and retrieve api data to localstorage 
Javascript :: jquery trigger link click 
Javascript :: javascript wait for function to return value 
Javascript :: redirect to another page javascript 
Javascript :: render html in node js 
Javascript :: javascript password max length 
Javascript :: install json-server 
Javascript :: how to edit /.prettierrc.json file pretter 
Javascript :: delete item from list javascript 
Javascript :: javascript array remove empty strings 
Javascript :: ERR_REQUIRE_ESM 
ADD CONTENT
Topic
Content
Source link
Name
6+2 =