Introduction SQL DataBASES Traditional Databases Relies on fixed table structure with rows, columns and keys Data is structured and easier to understand Examples: MySql, Sqlite, PostgreSQL
Introduction SQL VS NOSQL SQL NOSQL Relational Databases (RDBMS) Non-relational or distributed database Table based databases Document based, key-value pairs, graph databases or wide-column stores Predefined schema Dynamic schema for unstructured data Vertically scalable Both Vertically and Horizontally scalable Fit for complex query(Standard interface for performing complex query) Not fit for complex query(no standard interface) Eg : MySql, Oracle, Sqlite, Postgres and MS-SQL Eg : MongoDB, BigTable, Redis, RavenDb, Cassandra, Hbase, Neo4j and CouchDb
MONGO DB ? What IS
MOngo DB What is Mongo DB ? Mongo Db is a document database Each database contains Collections Each Collection contains documents Each Document can have varying number of fields The size and content of each document can be different
MONGO DB Common TeRms _id: The _id field is like the document's primary key. If you create a new document without an _id field, MongoDB will automatically create the field (24 digit unique identifier). Collection: A collection is the equivalent of a table which is created in any other RDMS such as Oracle or MS SQL Cursor: This is a pointer to the result set of a query. Clients can iterate through a cursor to retrieve results Document: A record in a MongoDB collection is basically called a document. The document in turn will consist of field name and values.
MONGODB Common Terms Field: A name-value pair in a document. A document has zero or more fields. Fields are analogous to columns in relational databases Database: This is a container for collections like in RDMS wherein it is a container for tables. JSON: This is known as JavaScript Object Notation. This is a human-readable, plain text format for expressing structured data.
MONGODb Why Mongo Db ? Document Oriented: It is very flexible and adaptable to real business world situation and requirements. Suitable for storing large amounts of data Ad Hoc Queries: Supports searching by field, range queries, and regular expression searches. Queries can be made to return specific fields within documents. Indexing: Indexes can be created to improve the performance of searches within MongoDB. Any field in a MongoDB document can be indexed
MONGODB WHY MONGO DB ? Replication : MongoDB can provide high availability with replica sets. A replica set consists of two or more mongo DB instances. Each replica set member may act in the role of the primary or secondary replica at any time. Load Balancing : MongoDB uses the concept of sharding to scale horizontally by splitting data across multiple MongoDB instances. MongoDB can run over multiple servers, balancing the load and/or duplicating data to keep the system up and running in case of hardware failure.
Mongo DB Commands Time to Start
Commands Creating Database Use Command is used to create database If the command is successful you will see the following use <Database Name>
Commands Creating A Collection The easiest way to create a collection is to insert a record into a new collection. If the collection is not there it will be automatically created If the command is executed successfully
Text Insert Array into Mongo Db Add more than one record at a time in mongo db
Commands Find Command This command is used to query and find relevant documents
Commands UPDATE Command Update Command is used to update a particular document
Commands Update Command To affect particular fields, we have to use operators such as $set, $inc, $unset, $rename
Commands Update command Additional properties can be passed. This includes upsert, multi etc. Each property enables a certain feature in the update query
Commands Aggregation Aggregation command is used for grouping based on a field and finding sum. It is also used to join two collections
Commands Remove Documents From Collection We can remove documents by using remove, deleteOne, deleteMany.
Commands Delete Collection and DB We use drop for deleting a collection and dropDatabase to delete database
Cursors, USERS, Replication And Sharding Extra Features Of Mongo
Features Cursors Cursors are pointers to the collections of objects. Using cursors we can programatically achieve a lot of things. For example we can iterate and print certain values we need.
Features User roles and Authentication Authentication can be enabled on Mongo Db for security purposes. You need to first create an Admin user with role userAdminAnyDatabase to default admin table. This admin user can then grant access to other user, create new users, provide access to certain collections etc.
Features Replication Replication in Mongo is carried out by ReplicaSets For high availability, mongo allows you replicated your data across different mongo server. First you need to start 2 server and specify they belong to the same replica set. Log in to one of the servers and add a new replica set with the ip of the second server. All data will then be synced and replicated.
Features Sharding There are two forms of scaling vertical and horizontal Mongo supports horizontal scaling by splitting the load between different replica sets. Three main components are present: Router server, Config server and the shard servers