MongoDB


14-11-19 – NoSQL(MongoDB)
Disadvantages of RDBMS
  • IO Ops take more time
  • Joins->make “read” slower as long as data staying in single machine.
  • Distributed Systems are scaleup to produce data. (scale-up, scale-out)
  1. Joins are expensive in DB Apps
  2. Rigid schema is a limitation
Solution for above is NOSQL->schema free distributed databases., Big Tables

NoSQL
In nosql, whole data will be store in very few tables (may be 1 or 2 tables). So that avoiding join data.
No schema

Disadvantage : there can be redundant data or duplicate of data because of no normalization applied on data. No data integrity

Two types of data bases in nosql
  1. OLTP (write heavy/ready)->Source Database
|
2. OLAP (read heavy)

Types of NoSQL
  1. Document–oriented (Ex : MongoDB, CouchDB ..)
  2. Column-family(wide-column) databases (HBase,Cassandra)
  3. Graph DBs (Neo4J, ..)
  4. Key value stores (memStores, ..)


SQL Vs NoSql
Schema oriented no schema follows
Relations (ex: joins) don’t have relation
Expensive comp to nosql not expensive


Partitions(diff data of same data)
Replica (same data of partition)

CAP(Consistency Availability Partitioning) Theorem

1.Partition tolerance provides replicas.
Shard(partition)
2.how replicas works
All the operation are go with primary replica. And creates op.log will be created and later point(with time gap) other replicas are affected.
  1. Availability
  • There should not be lag time b/w req and resp.
  1. Consistency
  • Lock should be applied on replicas(child) so that all clients should get same update.

From the above 3 points only any 2 points can be possible to make system is more reliable.

Install MongoDB
After that open command prompt, give following command to ensure mongodb working
C:>mongod
Server will be running on port 27017
Create one more cmd , give command mongo
C:>mongo
Curser will wait, then give command show dbs. This will sow all available dbs (ex: admin, local)

>show dbs
>db (what database currently working in)
>use sdb (switched to db sdb)
Ctrl+l -> to clear screen

Until data is not there in db, it will now show in list.
Putting collection in sdb


Commands for Insert or create:
1)insertOne =>db.users.insertOne({name: “Raju”, age: 44, gender: “male”})
2)insertMany => work with more than one document.
Db.users.insertMany([{},{},{}])

3) insert (not recommended to use) => db.users.insert()


To fetch the data from db
>db.users.findOne()  will give first matching document(one document)
>dn.users.find()  will give all matching documents

Three way we can connect with mongo db
1.mongo command line interface
2.GUI


Select:

  1. findOne => db.users.findOne({name: “Raju”})
  2. find => db.users.find({name: “Raju”})




Update:
  1. updateOne({filter},{what you want to update})
updateOne({name: “Raju”},{$set: {name: “kanakaraju”}})


  1. updateMany

MongoDB- Day2


mongod -> is a service (write operation)

import:

c:>mongoimport --db sdb --collection tvshows --file "D:\venkatesh\Data\tv-shows.json" --jsonArray

c:>mongoimport -d sdb -c addresses --type csv --headerline --file "D:\venkatesh\Data\addresses.csv"


mongoexport --db sdb --collection tvshows --out "D:\venkatesh\Data\tv-shows-export.json"
mongoexport --db sdb --collection addresses --out "D:\venkatesh\Data\addresses-export.csv" --type csv --fields "Name,Address,City,State"


ex:
(name like 'k%' and age>40)
OR
(age<20)

db.users.find({$or: [{age:{$lt:20},{name:/^k/,$age:{$gt:40}}]})

ex:
db.user.find({contacts: {$exists:true}})

db.budget.find({$expr: {$gt: ["$budget","$expenditure"]})
LookUP(Cursors)
Update

ISODate("2009-11-14 09:00:05z")
TimeStamp(-----)

UpdateOne -> to update based on condition, and update set of parmeters, if parmeter not matches then it will add as new parameter.

while creating collection _ can be directly accepted but - cannot allowed

ex:
db.emp_list.insertOne({name: "Venkatesh"}) //this is fine
db.emp-list.insertOne({name: "Venkatesh"}) //this is not-fine

to make allow - also
db.getCollection(emp-list).insertOne({name: "Venkatesh"}) //this is fine

to drop collection
db.getCollection(emp-list).drop()

No comments:

Post a Comment

JAVA NOTES

  click here  👉  Interview  Preparation : complete notes sql queries notes ============================================================ Con...