To watch the youtube video where I demo the code, click here.
Introduction: NoSQL
In another tutorial, we looked at using MySQL/SQL as our data storage persistence tool. Relational databases like MySQL hold data in tables, in a very structured format. However, NoSQL is a type of database where data isn't stored in a structured format: the data is stored in a very free unstructured way. Basically as JSON.
Why Use NoSQL?
So why would you use NoSQL as your database choice? Actually, NoSQL ends up being faster sometimes. Additionally, it takes less tables and commands to use the data stored in NoSQL databases. I like the example that tutorialspoint gives, so take a look at that webpage to see why NoSQL is better.
Mongo Is Pretty Popular
Mongo is a very popular NoSQL database. Where SQL databases have "tables", Mongo has "collections" of related data. Each entry in this collection is basically JSON with the relevant data in it.
In the next sections, we're going to look at how to install Mongo, and how to use it locally. Installing Mongo
To install Mongo on Ubuntu, use the following commands in your terminal. (Thanks to the article on Digital Ocean, for going in-depth about this installation.)
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927 echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list sudo apt-get update sudo apt-get install -y mongodb-org sudo systemctl start mongod
The above commands install and start mongo. However, the service will stop any time you shut your computer (Ubuntu) off.
To start the service so that you can use mongo (any time you restart the computer), run the following command in your terminal.
sudo systemctl start mongod
Using Mongo - The Commands
So we have Mongo installed, but now we need to use it. To run mongo, use the following command in your Ubuntu terminal. (And be sure the service is started.)
mongo
This will turn your linux command line into a mongo command line. You'll notice because the beginning of each line will change from something like "srcmake@ubuntu: ~/Documents/mongo $" to a plain and simple ">".
So now we're using mongo, what should we do? Let's go over some commands. (Thanks to a tutorial on scotch.io for going over these commands.)
First, check what individual databases we have.
show dbs
You may see one named "local", but let's create our own database. First, we use a DB named "srcmakeDB" (this command will create the DB if it doesn't exist), and then the "db" command will tell us which DB we're currently using.
use srcmakeDB db
So now our database is set up, but we need to create and edit some data. In SQL, we'd create a table, but for mongo, it's a little different...Remember that mongo stores unstructured data in (basically) JSON format.
In one command, we're going to create a "blogposts" collection (since it doesn't already exist), and add two blogposts to the collection.
db.blogposts.save([{title: 'how to hack', author: 'srcmake'}, {title: 'coding 101', likes: 102}]);
As you can see, one blogpost has a title and an author field, and the other has a title and a likes field. We save these to the blogposts collection (if it doesn't exist, then mongo will create it), and that happens in the DB that we're currently working in. (Which is srcmakeDB from the previous use command.)
Add one more blogpost to see how to add only one. (No [ ] brackets this time.)
db.blogposts.save({title: 'best coder ever', who: 'srcmake'});
So now we have three entries in our "blogposts" collection. How do we view them?
db.blogposts.find();
The above command will list all items in "blogposts". What if we want to specify some criteria to search better?
db.blogposts.find({author: 'srcmake'});
The above command specifies to find results where the author is 'srcmake'.
Updating an entry works in a similar manner. db.blogposts.update({author: 'srcmake'}, {author: 'make src'}); db.blogposts.find();
The first command, above, will search for an entry in blogposts where the author is "srcmake", and will change that entry to {author: "make src"}. (NOTE: It only affects the first match, not duplicates, and since we don't specify it, it will delete the {title: "how to hack"} part that we had before.
Speaking of deletion, to delete an entry... db.blogposts.remove({ author: 'make src' }); db.blogposts.find();
As you can see, the above command will search for an entry where the author is "make src", and then it will remove that entry.
To remove all blogposts:
db.blogposts.remove({});
And that's how you use mongodb in the command line. Conclusion
We've taken a look at what mongoDB is and how to use it on our own computer. It's super useful to know how to do this so that whenever you need to model a database or play with some data, you can locally test things out.
Obviously, you're most likely to use mongo in your Node (or other programming language) code, so keep an eye out for the tutorial I'll make for that topic.
The following video shows me demoing the code and explaining why it works.
Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels.
Twitter: @srcmake Discord: srcmake#3644 Youtube: srcmake Twitch: www.twitch.tv/srcmake Github: srcmake Comments are closed.
|
AuthorHi, I'm srcmake. I play video games and develop software. Pro-tip: Click the "DIRECTORY" button in the menu to find a list of blog posts.
License: All code and instructions are provided under the MIT License.
|