Mongo Mongo Pt. 1: INSTALL

Mama was queen of the mambo
Papa was king of the Congo
Deep down in the jungle
I started a database with Mongo

Prior to installing MongoDB, my only real experience with a database is using PostgreSQL. I’ve enjoyed the work i’ve done with SQL so far, and so only recently when I noticed a lot of demand for non-relational database expertise, did I decide to do a little tutorial with MongoDB. So here are some of the very basic things I learned about Mongo.

To install the MongoDB binaries, I used Homebrew. From the terminal, I entered a command of brew install mongodb. Then I had to use sudo to make a path to data/db in my root directory, since this is where MongoDB writes data by default. The command to accomplish this was sudo mkdir -p /data/db.

At this point I was still getting an error related to that path. There is more than one proper way to config MongoDB to prevent this error I believe. But I was in a position where I wanted to boot Mongo using a simple mongod command, and the solution I found through some stack overflow research was to run this command in the terminal…

sudo chmod 0755 /data/db && sudo chown $USER /data/db

In order to use Mongo, I needed to connect to the MongoClient. In my server file which I launch with Node, I needed to require MongoDB.MongoClient, and use the connect method to make this happen. Now was my cue to run that mongod command and to get my server up and running so that I could finish my config.

From the MongoDB docs I grabbed this url for my port mongodb://localhost:27017/myproject. I am now getting pretty close to being able to play around with this database. When all is said and done I end up with a configuration in my server file that looks like this.

var MongoClient = require('mongodb').MongoClient, assert = require('assert');
var url = 'mongodb://localhost:27017/myproject'
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
console.log("connected success to server");
insertDocuments(db, function() {
updateDocument(db, function() {
removeDocument(db, function() {
db.close()
})
})
});
});

What are these Document related methods that I have invoked here? To find out, tune in next time for Mongo Mongo Pt. 2: CRUD.

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.