Search
 
SCRIPT & CODE EXAMPLE
 

C

Mongo connect db

1	mongo # connects to mongodb://127.0.0.1:27017 by default
2	mongo --host <host> --port <port> -u <user> -p <pwd> # omit the password if you want a prompt
3	mongo "mongodb://192.168.1.1:27017"
4	mongo "mongodb+srv://cluster-name.abcde.mongodb.net/<dbname>" --username <username> # MongoDB Atlas
Comment

connect to mongo database

mongoose.connect(mongoURI);		
Comment

Connect mongodb server

const { MongoClient } = require("mongodb");
const connectionString = process.env.ATLAS_URI;
const client = new MongoClient(connectionString, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
});

let dbConnection;

module.exports = {
  connectToServer: function (callback) {
    client.connect(function (err, db) {
      if (err || !db) {
        return callback(err);
      }

      dbConnection = db.db("sample_airbnb");
      console.log("Successfully connected to MongoDB.");

      return callback();
    });
  },

  getDb: function () {
    return dbConnection;
  },
};
Comment

connect to mongodb

npm install mongodb --save
Comment

connect to mongodb

const MongoClient = require('mongodb').MongoClient
Comment

mongo db connection

const start = async () => {
    
    if (!process.env.DB_URI) {
        throw new Error('auth DB_URI must be defined');
    }
    try {
        await mongoose.connect(process.env.DB_URI!, {
            useNewUrlParser: true,
            useUnifiedTopology: true,
            useCreateIndex: true,
        });
        console.log('Server connected to MongoDb!');
    } catch (err) {
        throw new DbConnectionError();
        console.error(err);
    }

    const PORT = process.env.SERVER_PORT;
    app.listen(PORT, () => {
        console.log(`Server is listening on ${PORT}!!!!!!!!!`);
    });
};

start();
 Run code snippetHide results
Comment

PREVIOUS NEXT
Code Example
C :: sh: tailwindcss: command not found 
C :: c change value of const 
C :: c calculate median 
C :: pop and push shows black screen which needs to be pressed back flutter 
C :: print an int c 
C :: debian unhold packages 
C :: include ‘<stdlib.h’ or provide a declaration of ‘exit’ 
C :: iterate through enum in qt 
C :: implement crc using c language 
C :: mediawiki upload size 
C :: c structure with pointer 
C :: C Program to Check Whether Character is Lowercase or Not using islower function 
C :: 1000000000 
C :: bool c++ 
C :: atoi string to int 
C :: get docker 
C :: files in c programming 
C :: arduino dont working dc motor 
C :: type cast in c 
C :: Syntax for creating a node 
C :: Minimum Distance between words[AMAZON] 
C :: How to declare a string? 
C :: convert c to python online 
C :: Here is a program in C that illustrates the use of fscanf() to read a text file: 
C :: fraction sum c 
C :: when to add & in snacf c 
C :: C linked sorted lists 
C :: python to c converter online free 
C :: write varriable in file C 
C :: time to apply pmfby 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =