Connecting To MongoDB

Connecting To MongoDB


Connecting to MongoDB using the driver is primarily done using the MongoClient.connect method and a URI. Let’s look at how we connect to a couple of different server topologies.

Single Server Connection

We have a single MongoDB server instance running on the port 27017 Let’s connect using the driver and MongoClient.connect
var MongoClient = require('mongodb').MongoClient
  , assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log("Connected correctly to server");

  db.close();
});
Let’s break down the URI string we passed as the first argument to MongoClient.connect.
  • mongodb:// is the protocol definition
  • localhost:27017 is the server we are connecting to
  • /myproject is the database we wish to connect to
Connecting To MongoDB Connecting To MongoDB Reviewed by Anonymous on February 28, 2017 Rating: 5

No comments:

Java Ternary Operator

Java Ternary Operator Java ternary operator is the only conditional operator that takes three operands. Java ternary operator is a one l...

Powered by Blogger.