NoSQL (Not Only SQL)

NoSQL stores data in distribued machines.

Replication Model:

  • Master-slave replication one copy is a master copy, all writes are applied to master copy, read is done through slave nodes, no gaurantee that their values are up-to-date.
  • Master-master replication allows reads/writes at any of the replicas.

Sharding

Split a file and distribute into multiple nodes to distribute the load of accessing the file m=by multiple users

Sharding + replication of shards help load balancing and data availablity.

Types of NoSQL Systems

  • Documennt-based store data using well-known formats like JSON. E.g. MongoDB
  • Key-value systems Use ley-value paris for fast access. E.g. Amazon's DynamoDB, Facebook's Cassandra
  • Column-based Partitiona columns into column families, each family stored in its own file. E.g. Google's BigTable
  • Gaph-based data represented as graph. E.g. GraphBase

MongoDB

  • storing data in binary JSON (BSON) format
  • to create a collection of documents
db.createCollection(name, collection_options)
Example:
db.createCollection("posts", {capped:true, size:1310720, max:500})

MongoDB CRUD operations

  • Create create a document to be inserted into a collection: db.<collection_name>.insert(<documents>)
  • Read find a document in a collection: db.<collection_name>.find(<condtion>)
  • Update update a document: db.<collection_name>.update(<documents>)
  • Delete remove a document from a collection: db.<collection_name>.remove(<condition>)

MongoDB in action

Download MongoDB at https://www.mongodb.com/lp/download

  • Create a directory to store the files
    mkdir <direcory-name>
  • Launch the server
    mongodb --dbpath <directory-name>
    • Launch the client
      mongo

Useful commands

  • db
  • show dbs
  • use <db-name>
  • show collections
Example
> db.createCollection("employees", {'capped':true, size=10000, mX:50})

> show collections

> db.movies.insert({Name:'Alex', Age:37, City:'Detroit', Department: 'IT'})

> db.movies.insert({Name:'Bob', Age:25, City:'Detroit', Department: 'HR', Marital_status: 'Single'})

> db.movies.insert({Name:'John', Age:32, City:'An Arbor', Department: 'IT'})

Find records

> db.employees.find({City: 'Ann Arbor'})

> db.employees.find({Marital_status: {$exists: true}})

Query logical operators

  • $eq and $ne
  • $gt and $gte
  • $lt and $lte
  • $in and $nin
  • $or and $and
  • $not and $nor
  • $exists

In [ ]: