In object-oriented development, we're encouraged to approach code development through logical models, so that we can more readily conceptualise it in our mind. When we do this, we're better able discern the logical operations used to interact with it and information that it would contain at different times.
What if you could store the programmatic models almost exactly like you model them? What if you could store them as they are instead of in a series of rows in tables? By learning about MongoDB, you're going to be able to do just that!
In this series, we'll be learning everything from the basics of MongoDb, such as creating, updating and deleting databases and records, to being able to perform complex searches for data and elementary data mining with MapReduce. So, without much ado – let's get started!
Note: This tutorial is done from the perspective of NIX based system a la Mac OSX, Linux BSD and so on. But you should be able to follow along if you're running Windows pretty well as there are builds for most platforms.
Step 1 : Installing Mongo
Ok, so here's where the fun begins. We're going to get started by installing Mongo. Go to the MongoDb website and click on the downloads link.

This will bring you to a page where you can grab a build for your platform and architecture.

This tutorial only covers stable releases, so please do not grab a nightly build. Once it's downloaded, please install it as per the requirements of your platform.
If you're on a Nix machine, then please use its package manager to install the latest version for your platform.
With that out of the way, fire up a terminal and type in
mongo
. That will open up the Mongo shell and let us get under way. All being well, you'll see output similar to below:
If you see that, then you're ready to go.
Step 2 : Creating a Database/Inserting Records
Initially, no database is created. But don't worry, they'll instantly be created when we start inserting our records, which we're going to do right now. Copy the content below and paste it in your
mongo
shell
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
| db.nettuts.insert({ first: 'matthew' , last: 'setter' , dob: '21/04/1978' , gender: 'm' , hair_colour: 'brown' , occupation: 'developer' , nationality: 'australian' }); db.nettuts.insert({ first: 'james' , last: 'caan' , dob: '26/03/1940' , gender: 'm' , hair_colour: 'brown' , occupation: 'actor' , nationality: 'american' }); db.nettuts.insert({ first: 'arnold' , last: 'schwarzenegger' , dob: '03/06/1925' , gender: 'm' , hair_colour: 'brown' , occupation: 'actor' , nationality: 'american' }); db.nettuts.insert({ first: 'tony' , last: 'curtis' , dob: '21/04/1978' , gender: 'm' , hair_colour: 'brown' , occupation: 'developer' , nationality: 'american' }); db.nettuts.insert({ first: 'jamie lee' , last: 'curtis' , dob: '22/11/1958' , gender: 'f' , hair_colour: 'brown' , occupation: 'actor' , nationality: 'american' }); db.nettuts.insert({ first: 'michael' , last: 'caine' , dob: '14/03/1933' , gender: 'm' , hair_colour: 'brown' , occupation: 'actor' , nationality: 'english' }); db.nettuts.insert({ first: 'judi' , last: 'dench' , dob: '09/12/1934' , gender: 'f' , hair_colour: 'white' , occupation: 'actress' , nationality: 'english' }); |
All good? Excellent! To confirm that the database and accompanying records have been created, type in the following command:
1
| db.nettuts.find() |
If everything went to plan, then you will see the following output:

This shows that all of the records were created in the database. One thing to note before we go any further is the
id
field. This is auto generated by Mongo for you, if you don't specify an id. The reason is that every record must have a unique id
field.
You can see that we have one record for each of the ones that we insert – now we're ready to start querying them.
Step 3 : Searching For Records
You remember the previous command? It retrieved and displayed every record in the database. Helpful, yes, but how do you be more specific? How do you find all female actors, filtering out the males? That's a good question and the answer is selectors.
Selectors
Selectors are to Mongo what
where
clauses are to SQL. As with where clauses, Mongo selectors allow us to do the following:- specify criteria that
MUST
match. i.e., anAND
clause - specify criteria that
CAN
optionally match. i.e., anOR
clause - specify criteria that
MUST
exist - and much more...
Records That MUST Match
Let's start with a basic selector. Say that we want to find
. To accomplish that, you'll need to run the following command:
all actors that are female
. To accomplish that, you'll need to run the following command:
1
| db.nettuts.find({gender: 'f' }); |
Here we have specified that gender must be equal 'f'.
Running that command will return the following output:

What if we wanted to search for male actors? Run the following command:
1
| db.nettuts.find({gender: 'm' }); |
We'll get the following results:

No comments:
Post a Comment