MongoDB is a Document Oriented Database, a type of NoSQL database. It is open source and available at mongodb.com.

decrypt

Document Storage

Document databases pair each key with a complex data structure known as a document. Documents can contain many different key-value pairs, or key-array pairs, or even nested documents.

Mongo DB example

MongoDB stores data in flexible, JSON-like documents called collection. Each element of the database is stored as a separate JSON. Mongo can generate a database out of this file.

db.inventory.insertMany([
   // MongoDB adds the _id field with an ObjectId if _id is not present
   { item: "journal", qty: 25, status: "A" },
   { item: "notebook", qty: 50, status: "A" },
   { item: "paper", qty: 100, status: "D" },
   { item: "planner", qty: 75, status: "D" },
]);

Actions with Mongo

Mongo provides you with different type of actions to query the database, it follows the CRUD:

  • Insert: to add new element in the database
db.collection_name.insert({"fieldname":"value"})
  • Find: which lets you find and read elements in the database
db.inventory.find( {} )               // Will return all document in the collection
db.inventory.find( { status: "D" } )  // Searching filtering by a value
  • Update: to update the elements
db.collection_name.update()
  • Remove: to delete element of the database
db.collection_name.remove({"fieldname":"value"})

Deployment

The mongoDB can be deployed in different configuration:

  • Stand alone is a simple instance of the mongo DB, just the database.

  • Replica Set is a set of three database with one primary and two secondary.

    • If one is down then one of the two secondary gets elected to primary automatically.
    • All db have the same data in it.

monog-replicaset

  • Shard Cluster is composed of multiple replica set that shares the data of a collection (according to set rules).
    • The applications connect to the Database through a Mongo S application
    • The Mongo S will direct the request to the right replica set using a config replica set that knows where the information is stored.

mongo-shard