####What is a database? A database is an organized collection of data. They are usually organized in such a way that makes it easy to naviagte the data. You usually interact with them using DBMS like MySQL, PostgreSQL, Oracle. If you want to know more, you should probably Google it.


####DBMS? DBMS stands for database management system. Database management systems help…manage databases? Specifically they’re programs designed to help users interact with databases and the data stored within them. They usually contain the ability to do some CRUD operations.


####CRUD? The four operations every DBMS has to allow the user to manipulate the data.

  1. Create
  2. Read
  3. Update
  4. Delete

####What is a RDBMS? RDBMS stands for a relational database management system. A relational database is a database in which the data and how it’s related is stored.


####SQL Structured Query Language. A language designed to navigate RDBMS (Relational Database Management Systems).


####NoSQL vs SQL This debate is really centered around the concept that relational databases are not always the most efficient choice for data storage. The best explaination.


####NoSQL Database Types

  1. Document Based
  2. Key-Value Pairs
  3. Graph Stores
  4. Wide Column Stores

####Mongo DB Mongo DB is an open source, document based, NoSQL database.

DOWNLOAD

[OSX] [Ubuntu] [Red Hat] [Debian] [Windows]

####What’s Mongod?

Mongod is the primary daemon process for the MongoDB system. It handles data requests, manages data access, and performs background management operations. It runs in the background, and is what mongo needs to connect to.


####What’s a daemon?

In multitasking computer operating systems, a daemon is a computer program that runs as a background process, rather than being under the direct control of an interactive user.


###Mongo Basics

Start up an instance on mongo. If you followed the download tutorial, you should have a mongod service already running

mongod defaults to running on port 27017 and dbpath /data/db

 ~$ mongo 

To see all of your databases run > show dbs

To switch to working with a database, run ‘use dbName’. When booting up mongo, you have to specify which database you want to work in either by setting it when you run mongo or running use once mongo has started.

Mongo will also create the database if you try to use one that isn’t yet made.

> use test

And to show which database you’re currently working in…

> db

From within the mongo shell, you can do any of the 4 CRUD operations.


####Create Within each database, you can have collections (groupings of documents). They’re the equivalent to tables in RDBMS. Let’s start by creating a collection and inserting documents into it.

> me = { name: "Alisa", age: 23, dob: "09/06/1991" }

Then, insert the data into the collection by using

db.collectionName.insert(dataToInsert)

Just like with databases, Mongo will create the collection when you insert data into it if it doesn’t exist yet

So, to create the collection and insert your object run

> db.testData.insert(me)

####Read First, check that the collection was created

> show collections

Then, check to see that your document was added correctly

> db.testData.find()

Mongo adds in a unique id if you don’t specify one when adding the document.

{ "_id" : ObjectId("54502a3ddd9031dc7b0dbbb2"), "name" : "Alisa", "age" : 23, "dob" : "09/06/1991" }

Add some extra documents into your testData collection. To iterate over the documents in your collection, you can use a cursor object.

var c = db.testData.find()
while ( c.hasNext() ) printjson( c.next() )

You can also use array operations with the cursor object

> printJson( c[4] )

You can also also search for documents matching certain criteria.

> db.testData.find({ age: 23 })

To return a single document from a collection use findOne

> db.testData.findOne()

Or you can limit the amount of documents you query by adding limit and the amount after

> db.testData.find().limit(3)

####Update To update simply call update instead of insert.

db.testData.update({name: "Alisa"}, {name: "Uh-Lee-Suh"})

The update takes a query parameter first, in the same way find does. Then the update you want to make, and some extra options.

db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>
   }
)
  1. Upsert, when true, will create a new document with the data if nothing matches the query.
  2. Multi, when true, will update all requests that match the query.
  3. writeConcern is a document describing the guarentee Mongo will make upon completion of the update

####Delete Removing follows suit with the other CRUD operations, to remove documents from a collection just call remove and pass in a query to select what data to remove.

db.testData.remove({age:23})

Or to remove everything from a collection, pass in an empty object as the query

db.testData.remove({})