Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

CMPE

281 MongoDB Lab




I have tested all the steps on Mac. The Windows instructions should be similar.

You can refer to http://docs.mongodb.org/manual/tutorial/getting-started/ for
additional help with the lab.


Table of Contents

Download MongoDB ................................................................................................................................................. 2


Start MongoDB ............................................................................................................................................................ 2
Use the MongoDB Shell ........................................................................................................................................... 2
1. Starting the shell and one shard ................................................................................................................. 2
2. CRUD ....................................................................................................................................................................... 2
Sharding ......................................................................................................................................................................... 3
Using MongoDB with Java ...................................................................................................................................... 4
Lab ends ......................................................................................................................................................................... 5

CMPE 281 MongDBLab

M. Cherfaoui

Download MongoDB

Download MongoDB for your platform (Windows or Mac) from:


http://www.mongodb.org/downloads

Proceed with the install.

Start MongoDB

Create a work folder, e.g. mdb.

Start MongoDB:

./mongod --port 2000 --dbpath mdb --rest


In another terminal window, start the load balancing server:

./mongos configdb localhost:2000

Use the MongoDB Shell


MongoDB comes with a shell that allows configuring things and trying various
CRUD1 operations.
1. Starting the shell and one shard

In another terminal window, start the shell command line:

./mongo --host localhost --port 27017


Add a shard to Mongo:

mongos> db.getSisterDB("admin").runCommand ({addShard :


"localhost:2000", allowLocal : true });

2. CRUD

Create a MongoDB course collection and insert a document:

mongos> db.course.save ({name:"cloud computing - 1", teacher: "teacher
1", day: "Monday", website: "cc1.sjsu.edu"});


Note: MongoDB creates all collections in the default test database. You can create
your own databases but we will not do it in this lab.

1 Create, Read, Update, Delete
CMPE 281 MongDBLab

M. Cherfaoui


On the shell prompt, type db.course. then [tab]. This will list all the commands you
can run on the collection. Also, note that you can navigate the commands history
with the up and down arrow keys.

Insert another document in the course collection for cloud computing 2. This
time add an extra attribute (location = room 2). Note that MongDB does not
complain that the schema is different.

List all the records in the course collection.

Display the number of rows in the collection. It should be 2.

Remove row cloud computing 2 and display the number of rows again. It should
be 1 now.

Sharding

To experiment with sharding, we need at least 2 nodes. We will simulate another
node by running a second mongod instance on the same machine.

In another terminal, create a second work folder, e.g. mdb2. Start MongoDB with a
different port:

./mongod --port 2001 --dbpath mdb2 --rest


In the terminal where you are running the MongoDB shell, add another shard:

mongos> db.getSisterDB("admin").runCommand ({addShard :
"localhost:2001", allowLocal : true });


Enable sharding:

mongos> db.getSisterDB("admin").runCommand ({enablesharding : "test"})


Create a students collection and enable sharding on it:

mongos> db.getSisterDB("admin").runCommand ({shardcollection:


"test.students", key: {studentId : 1}})


Insert 1000 students:

mongos> for (var i = 1; i < 1000; i++) db.students.save ({studentId :
i, name : "student" + i});

This will assign the created documents to different shards. MongoDB actually
divides the key space into chunks and assigns chunks to shards.

CMPE 281 MongDBLab

M. Cherfaoui

To see the number of chunks, use:


db.getSisterDB("config").chunks.find().length();


And how chunks are defined:

db.getSisterDB("config").chunks.find().toArray ();

Using MongoDB with Java



Dont stop the MongoDB servers yet. You will need them running for this exercise.

Download the java driver from
http://central.maven.org/maven2/org/mongodb/mongo-java-driver/2.9.3/mongo-
java-driver-2.9.3.jar

Save the following class in a MongoJava.java file:

import com.mongodb.*;
public class MongoJava {
public static void main (String[] args) {
try {
Mongo mongo = new Mongo ("localHost", 27017);
DB db = mongo.getDB ("test");
DBCollection collection = db.getCollection ("course");
BasicDBObject searchQuery = new BasicDBObject();
searchQuery.put ("name", "cloud computing - 1");
DBCursor cursor = collection.find(searchQuery);
while (cursor.hasNext()) {
System.out.println ("Document: " + cursor.next());
}
} catch (Exception e) {
System.out.println (e.toString());
}
}
}


Run the java compiler:

$ javac -classpath mongo-java-driver-2.9.3.jar MongoJava.java


Run the program:

$ java -classpath mongo-java-driver-2.9.3.jar:. MongoJava


What does the program do?

CMPE 281 MongDBLab

M. Cherfaoui

Modify the program to add another document (name=Advanced cloud computing)


to the course collection.

Modify the program to search all the documents with name matching the cloud
computing pattern. Hint: use the java.util.regex.Pattern class as a search
parameter.

Lab ends


You can stop all the servers at this point.

CMPE 281 MongDBLab

M. Cherfaoui

You might also like