Be it a website, a desktop app, an android app, or a data related solution; what you will need is a database to store your data and RDBMS works just perfectly. The problem arises when you have huge data which you need to save in a structure but also want it to be fast enough to do continuous reads and writes. Normal RDBMS works perfectly, but the performance which No SQL provides on large scale is the reason to move in this direction. This provides a structure to data in a schemaless fashion using the document objects.
RDBMS vs Mongo DB(No SQL)
Mongo DB comes from the name humongous to denote the high amount of data it can handle. Mongo DB provide the scalability and flexibility using the schema less method. Using this you can store different kind of data in a single collection. But data still needs any format so that it can be saved and accessed accurately. This is done by saving the data in JSON objects.
In normal RDBMS OLTP systems, what we plan to do is normalize the data and try to keep everything in different tables which can be accessed using joins. But, in the No SQL we save everything in a single place called documents which are generally in JSON format.
This is what makes Mongo DB fast, like the RDBMS where a system needs to go to multiple collections to perform joins and merges, mongo db has all the data in a single place. This gives speed, performance and flexibility to the system.
architecture –
So, like our RDBMS we have a database in NoSQL as well. But, in that database, in place of tables we have collections. Those collections have documents in place of rows. These documents are schema less. Though we call it schema less, we will try to keep a structure to it because we want to keep some standards so that we can query that data. To do that we use the format called JSON.
The way we normalize the tables in the SQL to keep the data distributed and add a lot of relations in that, we try opposite of that in the NoSQL DB and save everything in same place. Moreover, we are not forced with any schema. We save a lot of time where we need to use relations to fetch data.
crud –
Create – USE can be used to create a database. If the database does not exists a new one will be created or else that will be set to the current database.
> use products
#switched to products db
db.products.insert( { item: “card”, qty: 15 } )
try {
db.products.insertOne( { item: “card”, qty: 15 } );
} catch (e) {
print (e);
};
try {
db.products.insertMany( [
{ item: “card”, qty: 15 },
{ item: “envelope”, qty: 20 },
{ item: “stamps” , qty: 30 }
] );
} catch (e) {
print (e);
}
db.collection.find( { qty: { $gt: 4 } } )
db.bios.find( { contribs: { $in: [ “ALGOL”, “Lisp” ]} } )
db.books.update(
{ stock: { $lte: 10 } },
{ $set: { reorder: true } },
{ multi: true }
)
#use update one or update many, because with update if $set is not used it will replace the document.
try {
db.restaurant.updateOne(
{ “name” : “Central Perk Cafe” },
{ $set: { “violations” : 3 } }
);
} catch (e) {
print(e);
}
try {
db.restaurant.updateMany(
{ violations: { $gt: 4 } },
{ $set: { “Review” : true } }
);
} catch (e) {
print(e);
}
db.products.remove( { qty: { $gt: 20 } } )
db.products.remove(
{ qty: { $gt: 20 } },
{ writeConcern: { w: “majority”, wtimeout: 5000 } }
)