Professional Documents
Culture Documents
Cmpe 281 Nosqldb Lab
Cmpe 281 Nosqldb Lab
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
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:
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:
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.
M. Cherfaoui
db.getSisterDB("config").chunks.find().length();
And
how
chunks
are
defined:
db.getSisterDB("config").chunks.find().toArray ();
Run
the
java
compiler:
Run
the
program:
$ java -classpath mongo-java-driver-2.9.3.jar:. MongoJava
What
does
the
program
do?
M. Cherfaoui
Lab ends
You
can
stop
all
the
servers
at
this
point.
M. Cherfaoui