Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

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;
  },
};
Source by www.mongodb.com #
 
PREVIOUS NEXT
Tagged: #Connect #mongodb #server
ADD COMMENT
Topic
Name
3+6 =