MongoDB - rohit120582sharma/Documentation GitHub Wiki

  • MongoDB is an open-source NoSql database.
  • It is a document-oriented database and stores data in Binary JSON (BSON) format.
  • It is a schemaless document store, but you should always use schemas if you don't want to go crazy.
  • In relational databases we have tables and rows, in MongoDB we have collections and documents. A document can contain sub-documents.
  • It is a cross platform database and can be installed across different platforms like Windows, Linux etc.
  • MongoDB Atlas is a cloud solution and Compass is a GUI tool to connect to database and look into it.

Features

  • MongoDB supports multiple Storage Engines. Storage Engines manages how data is saved in memory and on disk when we save data in form of documents.
  • Sharding is a major feature of MongoDB. Horizontal Scalability is possible due to sharding.
  • It provides an automatic failover mechanism, as data is restored through backup(replica) copy if server fails.
MongoDB Architecture


Connect to MongoDB

  1. Start the MongoDB server on a local server
$ mongod --config /usr/local/etc/mongod.conf --fork

# Get the list of all running process
$ ps aux | grep -v grep | grep mongod

# Kill any running process by PID
$ kill -9 <PID>

  1. You can use Shell to connect to running server
# start the Shell
$ mongo --port 27017

# Shell commands to use the database
> show dbs
> use <database>
> show collections
> db.<collection>.find()

  1. Use MongoDB driver for Node to connect to running MongoDB server from the node application
$ npm install mongodb --save
const MongoClient = require('mongodb').MongoClient;

// Connection URL
const url = 'mongodb://localhost:27017';

// Database Name
const dbName = 'myproject';

// Create a new MongoClient
const client = new MongoClient(url);

// Use connect method to connect to the Server
client.connect(function(err) {
  assert.equal(null, err);
  console.log("Connected successfully to server");

  const db = client.db(dbName);

  client.close();
});
⚠️ **GitHub.com Fallback** ⚠️