MongoDB Quiz

Connect with

Let’s suppose you have a five member replica set and want to assure that writes are committed to the journal and are acknowledged by at least 3 nodes before you proceed forward. What would be the appropriate settings for w and j?

a) w=1, j=1
b) w=”majority”, j=1
c) w=3, j=0
d) w=5, j=1
e) w=1,j=3

Ans: b

Which of the following statements are true about choosing and using a shard key:

a) The shard key must be unique
b) There must be a index on the collection that starts with the shard key.
c) Mongo can not enforce unique indexes on a sharded collection other than the shard key itself.
d) Any update that does not contain the shard key will be sent to all shards.
e) You can change the shard key on a collection if you desire.

Ans: b, c and d

You have a sharded system with three shards and have sharded the collections “grades” in the “test” database across those shards. The output of sh.status() when connected to mongos looks like this:

mongos> sh.status()
--- Sharding Status --- 
  sharding version: { "_id" : 1, "version" : 3 }
  shards:
 {  "_id" : "s0",  "host" : "s0/localhost:37017,localhost:37018,localhost:37019" }
 {  "_id" : "s1",  "host" : "s1/localhost:47017,localhost:47018,localhost:47019" }
 {  "_id" : "s2",  "host" : "s2/localhost:57017,localhost:57018,localhost:57019" }
  databases:
 {  "_id" : "admin",  "partitioned" : false,  "primary" : "config" }
 {  "_id" : "test",  "partitioned" : true,  "primary" : "s0" }
  test.grades chunks:
    s1 4
    s0 4
    s2 4
   { "student_id" : { $minKey : 1 } } -->> { "student_id" : 0 } on : s1 Timestamp(12000, 0) 
   { "student_id" : 0 } -->> { "student_id" : 2640 } on : s0 Timestamp(11000, 1) 
   { "student_id" : 2640 } -->> { "student_id" : 91918 } on : s1 Timestamp(10000, 1) 
   { "student_id" : 91918 } -->> { "student_id" : 176201 } on : s0 Timestamp(4000, 2) 
   { "student_id" : 176201 } -->> { "student_id" : 256639 } on : s2 Timestamp(12000, 1) 
   { "student_id" : 256639 } -->> { "student_id" : 344351 } on : s2 Timestamp(6000, 2) 
   { "student_id" : 344351 } -->> { "student_id" : 424983 } on : s0 Timestamp(7000, 2) 
   { "student_id" : 424983 } -->> { "student_id" : 509266 } on : s1 Timestamp(8000, 2) 
   { "student_id" : 509266 } -->> { "student_id" : 596849 } on : s1 Timestamp(9000, 2) 
   { "student_id" : 596849 } -->> { "student_id" : 772260 } on : s0 Timestamp(10000, 2) 
   { "student_id" : 772260 } -->> { "student_id" : 945802 } on : s2 Timestamp(11000, 2) 
   { "student_id" : 945802 } -->> { "student_id" : { $maxKey : 1 } } on : s2 Timestamp(11000, 3) 

If you ran the query

use test
db.grades.find({'student_id':530289})

Which shards would be involved in answering the query?

a) s0,s1 and s2
b) s0
c) s1
d) s2

Ans: c

Create three directories for the three mongod processes. On unix, this could be done as follows:

mkdir -p /data/rs1 /data/rs2 /data/rs3

Now start three mongo instances as follows. Note that are three commands. The browser is probably wrapping them visually.

./mongod --replSet m101 --logpath "1.log" --dbpath /data/rs1 --port 27017 --smallfiles --fork
./mongod --replSet m101 --logpath "2.log" --dbpath /data/rs2 --port 27018 --smallfiles --fork
./mongod --replSet m101 --logpath "3.log" --dbpath /data/rs3 --port 27019 --smallfiles --fork

Now connect to a mongo shell and make sure it comes up

./mongo --port 27017

Now you will create the replica set. Type the following commands into the mongo shell:

config = { _id: "m101", members:[
		{ _id : 0, host : "localhost:27017"},
		{ _id : 1, host : "localhost:27018"},
		{ _id : 2, host : "localhost:27019"} ]
	};
rs.initiate(config);

At this point, the replica set should be coming up. You can type

rs.status()

to see the state of replication.

m101:PRIMARY> rs.status()
{
        "set" : "m101",
        "date" : ISODate("2013-06-20T11:35:56Z"),
        "myState" : 1,
        "members" : [
                {
                        "_id" : 0,
                        "name" : "localhost:27017",
                        "health" : 1,
                        "state" : 1,
                        "stateStr" : "PRIMARY",
                        "uptime" : 195,
                        "optime" : Timestamp(1371728094000, 1),
                        "optimeDate" : ISODate("2013-06-20T11:34:54Z"),
                        "self" : true
                },
                {
                        "_id" : 1,
                        "name" : "localhost:27018",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 60,
                        "optime" : Timestamp(1371728094000, 1),
                        "optimeDate" : ISODate("2013-06-20T11:34:54Z"),
                        "lastHeartbeat" : ISODate("2013-06-20T11:35:54Z"),
                        "pingMs" : 0
                },
                {
                        "_id" : 2,
                        "name" : "localhost:27019",
                        "health" : 1,
                        "state" : 2,
                        "stateStr" : "SECONDARY",
                        "uptime" : 60,
                        "optime" : Timestamp(1371728094000, 1),
                        "optimeDate" : ISODate("2013-06-20T11:34:54Z"),
                        "lastHeartbeat" : ISODate("2013-06-20T11:35:54Z"),
                        "pingMs" : 1
                }
        ],
        "ok" : 1
}

Connect with

Leave a Comment

Your email address will not be published. Required fields are marked *