i like my coffee dark, strong and with nosql

hello again! well this time, the title kinda suggest what i’m going to talk about isn’t it? πŸ˜› oh well. when we were kids, we weren’t allowed to drink that much coffee. why? it made us a bit hyper and urm.. of course the tummy issues. *ehem*

but as we grew up, it came to a point where you couldn’t live without coffee. during our exam times, we needed something strong to keep us awake and alive. and well, a strong brew always did the trick.

same went with databases, we started off from sql databases, now our needs are in a larger scale, so, hello nosql!

what is nosql?

nosql databases, or “not only sql” databases is a technology where it caters a large set of distributed data. some time ago, an application being used by over 10,000 people at once was a rarity. but today, it has changed vastly. people talk in different terms now, big data, big users, internet of things, cloud computing etc. these aren’t the applications which deals with little amounts of records. people needed more, vast and dynamic structures to rely their data on. hence nosql came into play.

by 2020, it’s expected that 32 billion things connected to the internet, data usage will so vast. relational databases can’t cater such needs efficiently, dynamically and take use of the hardware capabilities of today. to read more on the expansion of data usage and the future of nosql, you go on couchbase. this article will help you understand the necessity of nosql dbs more and more.

advantages of nosql

  • nosql uses a schema less structure. this basically is a document based database where one collection may hold different documents. number of fields, content and the size may differ from one another.
  • nosql is more scalable than relational databases. this will cater large volumes of structured, semi-structured, and unstructured data.
  • rather than the spreadsheet type of data storage model used in sql databases, nosql uses key and values structure.Β  this enables the data to be stored in a single “document” method which nests values hierarchically.
  • sql databases have a fixed schema. to alter the structure, whole database has to be offline for a while to be updated. but in nosql, data schema is typically dynamic and is able to change on the go.
  • scaling of the database is horizontal. meaning, to expand the database, you just have to add more commodity servers or more cloud instances.
  • data manipulation is through object oriented apis. this means nosql databases is more applicable to day to day scenarios over sql databases.

these are some advantages of using nosql in general. in addition to these, the version of nosql i will be using, mongodb, has some more additional features like json style data storage, auto sharding, replication etc. you can read more on mongodb here at mongodb.org.

installing mongodb

installing mongodb is pretty straight forward. and hey, i didn’t mess up this time! yay me! πŸ˜€

you can download the latest version of mongodb at www.mongodb.org/downloads. as i’m explaining here how to install and configure it on linux, i’m using theΒ mongodb-linux-x86_64-ubuntu1404-3.0.4.tgz. this is for ubuntu 14.04.

once you have downloaded the tar ball, yep still on it πŸ˜› you can extract it to you softwares directory. for me it’s /home/nimila/installs/mongodb-linux-x86_64-ubuntu1404-3.0.4

this is better than installing these within system files because any alteration is easier, if you, urm.. tend to mess up easily like me. but yes, you can extract this to wherever you prefer.

mongodb executables are available inside the bin directory of the extracted tar ball. you can run the executables without mentioning the path by adding the bin directory to the path variable.

export PATH=/home/nimila/installs/mongodb-linux-x86_64-ubuntu1404-3.0.4/bin:$PATH

you might want to add this path to your bashrc or profile so you won’t have to run the above line each time you want to run mongodb.

we’re almost there. πŸ‘Š but before we can actually start mongodb, we have to create a data directory. by default, mongod process of mongodb uses /data/db directory. “but” this is not a must. you can create this folder wherever you want. i did this inside the extraction of mongodb. ex: /home/nimila/installs/mongodb-linux-x86_64-ubuntu1404-3.0.4/data/db

if you want to create the data directory in the default location, you can use the following command.

mkdir -p /data/db

else, create the directory wherever you want. if you do so, remember the path since you’ll need that to start the mongod process.

to start the mongodb run the following command.

mongod

if you have not included mongodb bin directory in the path variable, run the following command

/home/nimila/installs/mongodb-linux-x86_64-ubuntu1404-3.0.4/bin/mongod

**and if you created the data directory elsewhere, you’ll have to use the following command.

mongod --dbpath /home/nimila/installs/mongodb-linux-x86_64-ubuntu1404-3.0.4/data/db

once you get this correctly, you’ll see the following screen.

starting mongodb

starting mongodb

and this screen will stay as it is, because this terminal instance will be handling the mongodb server instance. which means, you won’t be able to actually work on this terminal instance.

so to work with the db, you’ll have to start new instance of the terminal, or open a tab inside the terminal. but make sure you won’t close the above instance at all times you’re using mongodb.

in the new terminal instance or tab you can run the following command to start the mongodb shell.

mongo

and as before, if you haven’t added the bin to path

/home/nimila/installs/mongodb-linux-x86_64-ubuntu1404-3.0.4/bin/mongo

now you’ll see the following screen.

mongo

mongodb shell

tadaaaaaa! we have successfully started with our mongodb! πŸŽ‰πŸ˜Š

now, to working with mongodb! *cracks knuckles*

in the shell, you can use the help command to check the basic help command of mongodb. so let’s start shall we.

creating/selecting database
creating or selecting a database in mongodb uses the same command.

use databases_name

if the entered database name is the name of an existing database, the system will select it for you. else, it will create a database in the given name.

view databases
to view databases, you can use the following command. but keep in mind, if you created a database and it’s empty, it won’t be shown in the results.

show dbs

to view the selected database run the following command.

db

**in mongodb, the default database is test.

remove databases
removing databases is fairly simple too. first make sure you’re in the database you want to remove/drop. so, first run use dbtoberemoved and select the db, then run the following command.

db.dropDatabase()

collections
collection in mongodb is kinda simillar to tables in sql databases. basically, a collection is grouping of mongodb documents. but collections doesn’t use a schema. documents within the collection can have different type of documents.

to create a collection, you can use the following command, but before you do, you might want to know about the parameters used while creating a collection too.

db.createCollection(name, options)

now, name here is a string and basically will be the name of the collection created. this parameter is a must. but, options is optional. (heh.. options is optional.. πŸ˜‹) options will be a document, which will specify the memory size and indexing involved.

if you’re to use options, following are available for you.

  • capped – boolean – if true, this will create a capped collection. this means you will create a collection of a given size. newer entries will overwrite the older entries to keep the size intact. if you select this as true, it’s a must to use the size option as well. make sense doesn’t it? πŸ˜›
  • autoIndexID – boolean – if you select this and set to true, this will add an index on _id field.
  • size – number – specifies the maximum size of the collection bytes.
  • max – number – specifies the maximum number of documents allowed in the collection.

simple enough yeah? so now let’s look at the how the code will look like.

db.createCollection("mycollection")

above will create a collection without any options included.

db.createCollection("mycollection", { capped : true, autoIndexID : true, size : 6142800, max : 1000 })

now this will create a collection with specified options, which will create a collection that is indexed, the size of 6142800 bytes and 1000 documents in size. got it? cool! πŸ‘

to view the collections of the selected database, you can use the following command.

show collections

also keep in mind, you don’t necessarily have to create collections. when you’re adding an document itself, you can create the collection.

db.mycollection.insert({"name" : "employees"})

here, mycollection will be the collection name while employees is the document name. simple enough yeah? 😊

so, how to drop a collection? pretty simple. before you remove a collection check for the available collections. then to remove/drop one, run the following command.

db.mycollection.drop()

aaaaand done!

inserting data
there are two ways to insert data into a collection in mongodb. that is insert() and save() method.

the syntax would look like this…

db.collectionname.insert(document)

here, collectioname refers to the collection you want the new document to be added to. document kinda acts like a tuple in a sql db. (not really though) πŸ˜›

db.mycollection.insert({
      _id: ObjectId(7df78ad8902c),
      title: 'mongodb introduction',
      description: 'how to use mongodb',
      by: 'turncoffeeintocode',
      url: 'https://turncoffeeintocode.wordpress.com',
      tags: ['mongodb', 'database', 'nosql','linux','comparisons'],
      postNo: 6
})

here, mycollection is the collection you’re inserting the document to. if the collection isn’t available, it will create the collection for you. _id is a 12 byte hexadecimal number that will be unique for each document. it will be consisting of the following.

_id: ObjectId(4 bytes timestamp, 3 bytes machine id, 2 bytes process id, 3 bytes increment)

but you don’t necessarily have to define this. because mongo will auto assign this if you don’t specify this yourself. (which is way easier.. πŸ˜› )

so what about adding multiple documents at once? it’s pretty easy.

db.mycollection.insert([
{
      title: 'mongodb introduction',
      description: 'how to use mongodb',
      by: 'turncoffeeintocode',
      url: 'https://turncoffeeintocode.wordpress.com',
      tags: ['mongodb', 'database', 'nosql','linux','comparisons'],
      postNo: 6
},
{
      title: 'mongodb data',
      description: 'how to add data to mongodb',
      by: 'turncoffeeintocode',
      url: 'https://turncoffeeintocode.wordpress.com',
      tags: ['mongodb', 'data'],
      postNo: 7,
      comments: [
      {
            user:'nimila',
            message: 'yay no sql!',
            dateCreated: new Date(2015,07,14,12,30),
      }
      ]
}
])

also, db.mycollection.save(document) will word the same way as above.

query data
to get data from mongodb, you have to use the find() method. syntax goes as follows.

db.collectionname.find()

here collectionname should be the collection you want to retrieve data from. this though, will display the data in a very uninstructed way where it’s almost unreadable. so, to make it readable, you can use the… urm… wait for it… the pretty() method. yup. pretty. πŸŒΈπŸ˜‚ this will look something like..

db.collectionname.find().pretty()

update data
to update a document, you can use update() method or save() method.

db.collectionname.update(SELECTIOIN_CRITERIA, UPDATED_DATA)

say i have following documents in my collection.

{ “_id” : ObjectId(5983548781331adf45ec5), “title”:”mongodb introduction”}
{ “_id” : ObjectId(5983548781331adf45ec6), “title”:”mongodb data”}

if i run the following command…

db.mycollection.update({'title':'mongodb introduction'},{$set:{'title':'mongodb introduction updated'}})

i will have the following in my collection if check my collection using db.mycollection.find()…

{ “_id” : ObjectId(5983548781331adf45ec5), “title”:”mongodb introduction updated”}
{ “_id” : ObjectId(5983548781331adf45ec6), “title”:”mongodb data”}

if you need to update all the documents since mongo only update one, you’ll need to update a parameter.

db.mycollection.update({'title':'mongodb introduction'},{$set:{'title':'mongodb introduction updated'}},{multi:true})

if you use the save() method, it’s a bit different. only a bit.. πŸ˜› it uses the object id specifically.

db.mycollection.save({"_id" : ObjectId(5983548781331adf45ec7), "title":"mongodb introduction updated", "by":"nimila"})

removing/deleting a document
to remove a document, we use remove() method. (thank you captain obvious!)

db.mycollection.remove(DELETION_CRITERIA)

say you have the following documents..

{ “_id” : ObjectId(5983548781331adf45ec5), “title”:”mongodb introduction”}
{ “_id” : ObjectId(5983548781331adf45ec6), “title”:”mongodb data”}

and you run the following command…

db.mycollection.remove({'title':'mongodb introduction'})

and the result will be…

{ “_id” : ObjectId(5983548781331adf45ec6), “title”:”mongodb data”}

tadaaaaaa! πŸ˜€

and if you want to only remove the first record.. you can use the following.

db.mycollection.remove(DELETION_CRITERIA,1)

or, if you want to truncate the collection (you know, remove all the documents from the collection..) you can use..

db.mycollection.remove()

so… that’s kinda the basic know how to mongodb, is this it? no of course not. you can do all the functions you used to do in sql here as well, and more too.

this post was basically to get to know and move around in mongodb with ease, you’ll get used to it, trust me.. nah nah.. shush… truuuust me.. πŸ˜› if you want to read more on mongo and it’s functions, you can always refer to their home site or mongodb tutorials on tutorialspoint.com.

so get brewing, go big! β˜•

spill a bit of coffee?