ict lesson 7 notes

Upload: manjulabs

Post on 04-Jun-2018

280 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 ICT Lesson 7 Notes

    1/14

    1

    ICT 4485 Lesson 7

    1. OverviewIn lesson 6 we started discussing Document databases. This week we will learn how to use

    MongoDB, one of the column databases. In the process we will review the document database

    data model.

    2. Checklisto Review Textbook Chapter 9 if necessaryo Read MongoDB Through the JavaScript Shell chapter of the MongoDB in action book by

    Kyle Banker (http://it-ebooks.info/book/964/)

    o Read the 4thChapter of The Definitive Guide to MongoDB by Eelco Plugge, PeterMembrey and Tim Hawkins (http://it-ebooks.info/book/966/)

    o Read the MongoDB CRUD Operations Documentation(http://docs.mongodb.org/manual/MongoDB-manual.pdf)

    o Read Lesson 7 Noteso Check the discussion board for Question/Comments, and post your own if you have anyo Complete the 3ndHomework Assignment.

    3. Using MongoDB3.1. Starting MongoDB

    Starting MongoDB is quite easy. You need to know where you uncompressed / unzipped

    the MongoDB package you downloaded. Within the uncompressed folder find the bin

    folder. There will be a couple of files in that folder that we will need: mongod.exe and

    mongo.exe. The mongod.exe file is the server executable, so double click on that one first.

    You should get a window up that looks something like Figure 1. After you have the server

    piece up, double click on the mongo.exe executable, which will start the client. The client

    should look like Figure 2.

    http://it-ebooks.info/book/964/http://it-ebooks.info/book/964/http://it-ebooks.info/book/964/http://it-ebooks.info/book/966/http://it-ebooks.info/book/966/http://it-ebooks.info/book/966/http://docs.mongodb.org/manual/MongoDB-manual.pdfhttp://docs.mongodb.org/manual/MongoDB-manual.pdfhttp://docs.mongodb.org/manual/MongoDB-manual.pdfhttp://docs.mongodb.org/manual/MongoDB-manual.pdfhttp://it-ebooks.info/book/966/http://it-ebooks.info/book/964/
  • 8/14/2019 ICT Lesson 7 Notes

    2/14

    2

    Figure 1

    Figure 2

  • 8/14/2019 ICT Lesson 7 Notes

    3/14

    3

    3.2. Oil Change Data ModelAt this point, lets revisit the scenario from the previous lesson and add some more data

    to our project. Here is data that needs to be entered into the database:

    Owner Information

    First Last Street City ZIP State Car

    Bob Smith 123 Main St. Somewhereville 80999 CO 588VJK3094JKFKJSD

    Ann Brown 2314 Broadway Somewhereville 80998 CO 47384FJKJ1298JSD23

    Jill Jones 493 1St St. Supertown 80997 CO 8493JFKDJ19283SDH

    874JGF34989NFS382

    Bill Adams 848 47th St. Somewhereville 80999 CO 8DS302JDKJ0238812

    Car Information

    Car ID Make Model Trim and

    Engine

    Milage Next Service

    Due

    588VJK3094JKFKJSD VW Jetta 1.8T 89285 3/25/2014

    47384FJKJ1298JSD23 Toyota Camry XLE 42851 1/4/2014

    8493JFKDJ19283SDH VW Passat 2.0D 99431 1/6/2014

    8DS302JDKJ0238812 Subaru Legacy 2.5 59041 2/5/2014

    874JGF34989NFS382 BMW X3 57124 1/15/2014

  • 8/14/2019 ICT Lesson 7 Notes

    4/14

    4

    Invoice Information

    From the discussion of last week the date structure will look as follows:

    Car Collection

    {

    CarID : 588VJK3094JKFKJSD

    Car Make : VW,

    Car Model:Jetta,

    Trim and Engine : 1.8 T,

    Mileage at Last Service: 89285,

    Due for Next Service: 3/25/2014,

    Owner:{

    First Name : Bob,

    Last Name: Smith,

    Address :

    {

    street : 123 Main St.,

    Invoice ID Car ID Service

    Date

    Services Parts

    167540983 588VJK3094JKFKJSD 3/25/2012 Oil Replacement : 30,Windshield Wiper

    Replacement: 10

    Oil : 20,Windshield Wipers : 60

    4980987883 588VJK3094JKFKJSD 9/25/2013 Oil Replacement : 30,

    Oil Filter

    Replacment: 20,

    Oil : 20,

    Oil Filter : 35

    847382974 8DS302JDKJ0238812 4/15/2013 Oil Replacement : 30 Oil : 20

    238478742 588VJK3094JKFKJSD 5/2/2013 Oil Replacement : 30,

    Oil Filter

    Replacment: 20,

    Oil : 20,

    Oil Filter : 35

    438472937 47384FJKJ1298JSD23 5/24/2012 "Windshield Wiper

    Replacement: 10

    Windshield Wipers : 60

    234878724 8DS302JDKJ0238812 12/28/2012 Oil Replacement : 30,

    "Air Filter

    Replacment: 20,

    Oil : 20,

    "Air Filter : 55

    489348023 8DS302JDKJ0238812 3/25/2012 Oil Replacement : 30 Oil : 20

    489348024 874JGF34989NFS382 9/25/2013 Oil Replacement : 30,

    Oil Filter

    Replacment: 20,

    Oil : 20,

    Oil Filter : 35

  • 8/14/2019 ICT Lesson 7 Notes

    5/14

    5

    city : Somewhereville,

    state : CO,

    ZIP : 80999

    },

    },

    }

    Invoice Collection

    {

    InvoiceID : 167540983,

    CarID : 588VJK3094JKFKJSD,

    Service Date: 3/25/2012,

    Service Charges:

    {

    Oil Replacement : 30,

    Windshield Wiper Replacement: 10,

    },

    Parts :

    {

    Oil : 20,

    Windshield Wipers : 60,

    },

    }

    {

    InvoiceID : 4980987883,

    CarID : 588VJK3094JKFKJSD,

    Service Date: 9/25/2013,

    Service Charges:

    {

    Oil Replacement : 30,

    Oil Filter Replacment: 20,

    },

    Parts :

    {Oil : 20,

    Oil Filter : 35,

    },

    }

  • 8/14/2019 ICT Lesson 7 Notes

    6/14

    6

    3.3. Adding, Deleting, Updating Records Methods and GeneralCommands

    To use a database you would use the use command followed by the database name. Lets

    call our database CarService:

    use CarService

    This does not create a database just lets MongoDB know that the documents you will be

    adding need to go into the CarService database. Once you start adding the documents

    MongoDB will create the database by itself. By the same token we do not need to create a

    collection either. We can just start adding records to a collection. In order to do that we

    need to use the insert method. We will need to preface the insert method with the db

    designation and the name of the collection we want to add the document to. We also need

    to provide the insert method with data we need to addthe actual document we want to

    add. Lets add a simple record:

    db.customer.insert({"FirstName":" Bob", "LastName":"Smith",

    "Street":"123 Main St.", "City":"Somewhereville",

    "State":"CO", "ZIP":"80999"})

    We can search for the records using the find method:

    db.customer.find()

    The result should look something like shown in figure 3:

    Figure 3

    Note that along with the record you entered, an additional field has been created the _id

    field of ObjectId type. In the case above the value of the key is

    525f679259a127dd8a2b8682.

    If we wanted to add a car ID to this owner we could use the update method. With the

    update method we need to supply two parametersthe first parameter will serve to find

  • 8/14/2019 ICT Lesson 7 Notes

    7/14

    7

    the record, the second one to add the new value we want to add. We also need to use the

    $set operator:

    db.customer.update({"_id":

    ObjectId("525f679259a127dd8a2b8682")}, {$set:

    {"carID":"588VJK3094JKFKJSD"}})

    If you want to modify a value you would do it the same way, except of course that you

    would specify an existing field . If you do not specify the $set operator, the whole record

    will get overwritten. For example, if you issue the following command:

    db.customer.update({"_id":

    ObjectId("525f679259a127dd8a2b8682")}, {FirstName:"Robert

    Bobby Jones"})

    you will lose all other data values, and the newly set FirstName field is the only field that

    will be in the record.

    When it comes to deletions you can delete anythingfrom a part of a document to a whole

    collection. The remove method is used to delete data. The remove method can take a field

    and value you want to search for. To delete the above record we could issue a command

    like this:

    db.customer.remove({"_id" :

    ObjectId("525f679259a127dd8a2b8682")})

    You can also look up how many documents are in the collection, so we can do that to find

    out if indeed our document has been deleted:

    db.customer.count()

    Since the document we just deleted was the only document in our collection so far, the

    result of this query will be 0. You could also give a parameter to the count method, for

    example:

    db.customer.count({FirstName: Robert)

    in which case only customers with the first name of Robert would be counted.

    Just because we deleted all the records in the collection, that does not take the collection

    itself away. You can check what all collections are available with the following command:

    show collections

    Among others the customer collection will be listed. If you want to get rid of the collection

    itself you can use the drop method.

    db.customer.drop()

  • 8/14/2019 ICT Lesson 7 Notes

    8/14

    8

    You can also check what database exist in the system by issuing the following statement:

    show dbs

    3.4. Searching / Retrieving RecordsIn the previous section we used the find method to show all records in the collection.You

    could also provide the find method a fieldvalue pair you want to search for. In order to

    search for anything we need to add a some records. I am posting the code to add a single

    record that matches our database schema here, but the statements that add the rest of the

    data are in the text file posted under Doc Sharing for this lesson.

    db.car.insert(

    {

    "CarID" : "588VJK3094JKFKJSD",

    "CarMake" : "VW",

    "TrimAndEngine" : "1.8 T",

    "MileageatLastService" : 89285,

    "DueForNextService" : new Date(3/25/2014),

    "Owner":

    {"FirstName" : "Bob",

    "LastName" : "Smith",

    "Address":

    {

    "Street":"123 Main St.",

    "City":"Somewhereville",

    "State":"CO",

    "ZIP":"80999"

    },

    },

    }

    )

  • 8/14/2019 ICT Lesson 7 Notes

    9/14

    9

    Again,

    db.car.find()

    will return all records in the car collection. If you wanted to find only VW cars, you couldissue a query like this:

    db.car.find({CarMake: "VW"})

    You can search in subdocuments as well:

    db.car.find({"Owner.LastName" : "Smith"})

    There is more than one VW, so if you wanted to query your records by more than one

    criteria, to refine your results, you would do it like this:

    db.car.find({CarMake : "VW", "Owner.LastName" : "Smith"})

    Note that multiple criteria are separated by a comma and are within a single block wrapped

    by {}.

    If you wanted to return only the CarMake, TrimAndEngine and MilageLastService fields, i.e.

    create a projection, you would specify them, like this:

    db.car.find({CarMake : "VW", "Owner.LastName" : "Smith"},{CarMake: 1, TrimAndEngine: 1, MileageLastService:1})

    In this case we added a second block separated by {} containing the fields we want to showalong with a :1. The number 1 is a flag to MongoDB that you want to show these field. If you

    wanted to show all but these fields, you would use a 0 instead of the one, and all fields but

    the ones specified would be shown. Selecting only certain fields to be returned is called

    projection.

    You can also sort through the results by using the sort method:

    db.car.find({CarMake : "VW", "Owner.LastName" : "Smith"},

    {CarMake: 0, TrimAndEngine: 0,MilageLastService:0}).sort({"Owner.LastName" : 1})

    The sort methods takes the field you want to sort your records by. The number 1 here is a

    flag to MongoDB to sort the records in ascending order. You would use -1 to sort them in

    descending order.

  • 8/14/2019 ICT Lesson 7 Notes

    10/14

    10

    There are a number of cursor methods. The table below shows the listing from MongoDB

    documentation1:

    Cursor Methods

    Name Description

    cursor.addOption()Adds special wire protocol flags that modify the behavior of

    the query.

    cursor.batchSize()Controls the number of documents MongoDB will return to

    the client in a single network message.

    cursor.count() Returns a count of the documents in a cursor.

    cursor.explain() Reports on the query execution plan, including index use,for a cursor.

    cursor.forEach() Applies a JavaScript function for every document in a cursor.

    cursor.hasNext()Returns true if the cursor has documents and can be

    iterated.

    cursor.hint() Forces MongoDB to use a specific index for a query.

    cursor.limit() Constrains the size of a cursors result set.

    cursor.map()Applies a function to each document in a cursor and collects

    the return values in an array.

    cursor.max()Specifies an exclusive upper index bound for a cursor. For

    use withcursor.hint()

    cursor.min()Specifies an inclusive lower index bound for a cursor. For

    use withcursor.hint()

    cursor.next() Returns the next document in a cursor.

    cursor.objsLeftInBatch() Returns the number of documents left in the current cursor

    batch.

    1http://docs.mongodb.org/manual/reference/method/js-cursor/

    http://docs.mongodb.org/manual/reference/method/cursor.addOption/#cursor.addOptionhttp://docs.mongodb.org/manual/reference/method/cursor.batchSize/#cursor.batchSizehttp://docs.mongodb.org/manual/reference/method/cursor.count/#cursor.counthttp://docs.mongodb.org/manual/reference/method/cursor.explain/#cursor.explainhttp://docs.mongodb.org/manual/reference/method/cursor.forEach/#cursor.forEachhttp://docs.mongodb.org/manual/reference/method/cursor.hasNext/#cursor.hasNexthttp://docs.mongodb.org/manual/reference/method/cursor.hint/#cursor.hinthttp://docs.mongodb.org/manual/reference/method/cursor.limit/#cursor.limithttp://docs.mongodb.org/manual/reference/method/cursor.map/#cursor.maphttp://docs.mongodb.org/manual/reference/method/cursor.max/#cursor.maxhttp://docs.mongodb.org/manual/reference/method/cursor.hint/#cursor.hinthttp://docs.mongodb.org/manual/reference/method/cursor.hint/#cursor.hinthttp://docs.mongodb.org/manual/reference/method/cursor.hint/#cursor.hinthttp://docs.mongodb.org/manual/reference/method/cursor.min/#cursor.minhttp://docs.mongodb.org/manual/reference/method/cursor.hint/#cursor.hinthttp://docs.mongodb.org/manual/reference/method/cursor.hint/#cursor.hinthttp://docs.mongodb.org/manual/reference/method/cursor.hint/#cursor.hinthttp://docs.mongodb.org/manual/reference/method/cursor.next/#cursor.nexthttp://docs.mongodb.org/manual/reference/method/cursor.objsLeftInBatch/#cursor.objsLeftInBatchhttp://docs.mongodb.org/manual/reference/method/cursor.objsLeftInBatch/#cursor.objsLeftInBatchhttp://docs.mongodb.org/manual/reference/method/js-cursor/http://docs.mongodb.org/manual/reference/method/js-cursor/http://docs.mongodb.org/manual/reference/method/js-cursor/http://docs.mongodb.org/manual/reference/method/js-cursor/http://docs.mongodb.org/manual/reference/method/cursor.objsLeftInBatch/#cursor.objsLeftInBatchhttp://docs.mongodb.org/manual/reference/method/cursor.next/#cursor.nexthttp://docs.mongodb.org/manual/reference/method/cursor.hint/#cursor.hinthttp://docs.mongodb.org/manual/reference/method/cursor.min/#cursor.minhttp://docs.mongodb.org/manual/reference/method/cursor.hint/#cursor.hinthttp://docs.mongodb.org/manual/reference/method/cursor.max/#cursor.maxhttp://docs.mongodb.org/manual/reference/method/cursor.map/#cursor.maphttp://docs.mongodb.org/manual/reference/method/cursor.limit/#cursor.limithttp://docs.mongodb.org/manual/reference/method/cursor.hint/#cursor.hinthttp://docs.mongodb.org/manual/reference/method/cursor.hasNext/#cursor.hasNexthttp://docs.mongodb.org/manual/reference/method/cursor.forEach/#cursor.forEachhttp://docs.mongodb.org/manual/reference/method/cursor.explain/#cursor.explainhttp://docs.mongodb.org/manual/reference/method/cursor.count/#cursor.counthttp://docs.mongodb.org/manual/reference/method/cursor.batchSize/#cursor.batchSizehttp://docs.mongodb.org/manual/reference/method/cursor.addOption/#cursor.addOption
  • 8/14/2019 ICT Lesson 7 Notes

    11/14

    11

    Name Description

    cursor.readPref()Specifies aread preferenceto a cursor to control how the

    client directs queries to areplica set.

    cursor.showDiskLoc()Returns a cursor with modified documents that include the

    on-disk location of the document.

    cursor.size()Returns a count of the documents in the cursor after

    applyingskip()andlimit()methods.

    cursor.skip()Returns a cursor that begins returning results only after

    passing or skipping a number of documents.

    cursor.snapshot()

    Forces the cursor to use the index on the _idfield. Ensures

    that the cursor returns each document, with regards to the

    value of the _idfield, only once.

    cursor.sort() Returns results ordered according to a sort specification.

    cursor.toArray()Returns an array that contains all documents returned by

    the cursor.

    You can also use operators like greater then, less then, etc. Lets supposed we wanted tofind all VW vehicles that had the mileage greater than 90,000 at the last service we could

    write a query like this:

    db.car.find({CarMake : "VW", MileageatLastService:{$gt:90000}}, {CarMake: 0, TrimAndEngine: 0,

    MilageLastService:0}).sort({"Owner.LastName" : 1})

    There are a number of query and projection modifiers. For a complete list with explanations

    look at :http://docs.mongodb.org/manual/reference/operator/query/ .

    3.5. AggregationThere are also a number of aggregation operators, which you can find at

    http://docs.mongodb.org/manual/reference/operator/aggregation/. Aggregation operators

    are comparable to SQL aggregation operatorssum, avg, min, max, etc. Here is an example

    http://docs.mongodb.org/manual/reference/method/cursor.readPref/#cursor.readPrefhttp://docs.mongodb.org/manual/reference/glossary/#term-read-preferencehttp://docs.mongodb.org/manual/reference/glossary/#term-read-preferencehttp://docs.mongodb.org/manual/reference/glossary/#term-read-preferencehttp://docs.mongodb.org/manual/reference/glossary/#term-replica-sethttp://docs.mongodb.org/manual/reference/glossary/#term-replica-sethttp://docs.mongodb.org/manual/reference/glossary/#term-replica-sethttp://docs.mongodb.org/manual/reference/method/cursor.showDiskLoc/#cursor.showDiskLochttp://docs.mongodb.org/manual/reference/method/cursor.size/#cursor.sizehttp://docs.mongodb.org/manual/reference/method/cursor.skip/#cursor.skiphttp://docs.mongodb.org/manual/reference/method/cursor.skip/#cursor.skiphttp://docs.mongodb.org/manual/reference/method/cursor.skip/#cursor.skiphttp://docs.mongodb.org/manual/reference/method/cursor.limit/#cursor.limithttp://docs.mongodb.org/manual/reference/method/cursor.limit/#cursor.limithttp://docs.mongodb.org/manual/reference/method/cursor.limit/#cursor.limithttp://docs.mongodb.org/manual/reference/method/cursor.skip/#cursor.skiphttp://docs.mongodb.org/manual/reference/method/cursor.snapshot/#cursor.snapshothttp://docs.mongodb.org/manual/reference/method/cursor.sort/#cursor.sorthttp://docs.mongodb.org/manual/reference/method/cursor.toArray/#cursor.toArrayhttp://docs.mongodb.org/manual/reference/operator/query/http://docs.mongodb.org/manual/reference/operator/query/http://docs.mongodb.org/manual/reference/operator/query/http://docs.mongodb.org/manual/reference/operator/aggregation/http://docs.mongodb.org/manual/reference/operator/aggregation/http://docs.mongodb.org/manual/reference/operator/query/http://docs.mongodb.org/manual/reference/method/cursor.toArray/#cursor.toArrayhttp://docs.mongodb.org/manual/reference/method/cursor.sort/#cursor.sorthttp://docs.mongodb.org/manual/reference/method/cursor.snapshot/#cursor.snapshothttp://docs.mongodb.org/manual/reference/method/cursor.skip/#cursor.skiphttp://docs.mongodb.org/manual/reference/method/cursor.limit/#cursor.limithttp://docs.mongodb.org/manual/reference/method/cursor.skip/#cursor.skiphttp://docs.mongodb.org/manual/reference/method/cursor.size/#cursor.sizehttp://docs.mongodb.org/manual/reference/method/cursor.showDiskLoc/#cursor.showDiskLochttp://docs.mongodb.org/manual/reference/glossary/#term-replica-sethttp://docs.mongodb.org/manual/reference/glossary/#term-read-preferencehttp://docs.mongodb.org/manual/reference/method/cursor.readPref/#cursor.readPref
  • 8/14/2019 ICT Lesson 7 Notes

    12/14

    12

    of how we could use one to determine the car with the highest number of miles at the last

    service:

    db.car.aggregate([{$group: {_id:0, maxMilage: {$max:

    "$MilageatLastService"}}}])

    The syntax here is different than in SQL, but we are using a similar procedure. We arespecifying the aggregation function, but we are also grouping our records. The above query

    groups all the records, but if you wanted to group your records by a certain field you would

    write your query like this:

    db.car.aggregate([{$group: {_id:"$CarMake", maxMilage: {$max:

    "$MileageatLastService"}}}])

    You can also select only certain records to be aggregated by using the $match operator. In

    the example below we are showing results only for the Subaru cars:

    db.car.aggregate( [ { $match: { CarMake: "Subaru" } }, {

    $group: { _id: 0, maxMilage: { $max: "$MilageatLastService"} }} ] )

    3.6. Arrays and Other OperatorsIn our scenario we do not have any need to use arrays, so lets use a different scenario for

    that. Lets suppose you wanted to keep track of students and their grades in your database.

    You could add a sample record like this:

    db.student.insert({First:"Bob", Last:"Smith", Grades:

    [82,95,98,100, 96, 89]})

    Lets suppose the student completed another assignment and we want to add the grade

    into the array. We would do it like this:

    db.student.update({Last: "Smith"}, {$addToSet: {Grades: 100}})

    Like in some of the prior examples we use the first argument of the update method to

    specify which record we want to update, and the second argument with the update we

    want to perform. This time we are using the $addToSet operator to add the value to the

    array. Arrays have a number of other operators, which you can find at

    http://docs.mongodb.org/manual/reference/operator/update-array/.

    Arrays are not the only data type you can use operators on. There are operators you can use

    on other fields and you can find them at:

    http://docs.mongodb.org/manual/reference/operator/update-field/ .

    http://docs.mongodb.org/manual/reference/operator/update-array/http://docs.mongodb.org/manual/reference/operator/update-array/http://docs.mongodb.org/manual/reference/operator/update-field/http://docs.mongodb.org/manual/reference/operator/update-field/http://docs.mongodb.org/manual/reference/operator/update-field/http://docs.mongodb.org/manual/reference/operator/update-array/
  • 8/14/2019 ICT Lesson 7 Notes

    13/14

    13

    3.7. Handling RelationshipsThere are a couple of ways to work with relationships in MongoDB. One of them involves

    using DBRef. The MongoDB documentation recommends using manual references, so that

    will be the methodology we will be using in this class.

    2

    Using manual references involves including the objectID of one document in a second

    document. In our scenario we have two document collectionscar and invoice. We will add

    an invoice with the _id of the appropriate car, so we can link the two. Since we let the _id

    fields be system generated, we first need to find the _id of a specific car:

    db.car.find({CarID: "588VJK3094JKFKJSD"})

    It so happens that in my case the _id for that vehicle is

    ObjectId("525f7e939831fa69326e3cd0"), so I will add that _id to a new field in an invoice

    record for that car:

    db.invoice.update({CarID:"588VJK3094JKFKJSD"}, {$set:{Car_oid: ObjectId("525f7e939831fa69326e3cd0")}})

    Now if I pulled an invoice and wanted to use that car_oid to find out more information

    about the car from the car collection, I would issue a couple of commands:

    invoice = db.invoice.findOne({CarID: "588VJK3094JKFKJSD"})

    db.car.find({_id : invoice.Car_oid})

    If you are wondering if we need the CarID in the invoice collection, the answer is no the

    Car_oid is the field we would use to link the two collections.

    Keep in mind that MongoDB does not maintain relational integrity, so your application

    needs to manage that. Meaning, if you chose to delete the document from the car collection

    with CarID 588VJK3094JKFKJSD, and you wanted all references for that car to be gone from

    the database, you will also need to delete the appropriate record(s) from the invoice

    collection.

    In this case if we wanted to remove the previously mentioned car we would issue a remove

    command using the Car_oid for all the pertinent invoices and use the same ObjectID to

    remove the car from the car collection.

    3.8. IndexingMongoDB supports a number of index types, which you can familiarize yourself with at

    http://docs.mongodb.org/manual/core/indexes-introduction/ .The simplest type of index

    2http://docs.mongodb.org/manual/reference/database-references/

    http://docs.mongodb.org/manual/core/indexes-introduction/http://docs.mongodb.org/manual/core/indexes-introduction/http://docs.mongodb.org/manual/reference/database-references/http://docs.mongodb.org/manual/reference/database-references/http://docs.mongodb.org/manual/reference/database-references/http://docs.mongodb.org/manual/reference/database-references/http://docs.mongodb.org/manual/core/indexes-introduction/
  • 8/14/2019 ICT Lesson 7 Notes

    14/14

    14

    you can use is a single field index, which is very much analogous to a single field index you

    would have used in a relational database. Lets suppose you wanted to create a secondary

    index on our database on the CarMake fieldyou would do it like this:

    db.car.ensureIndex({CarMake:1})

    3.9. Setting up replication and shardingMongoDB supports sharding and replication, but both need to be set up. In order to

    set up sharding you first need to set up configuration servers.

    The config server processes aremongodinstances that store the clusters metadata.

    You designate amongodas a config server using the --configsvroption. Each config

    server stores a complete copy of the clusters metadata.

    In production deployments, you must deploy exactly three config server instances,each running on different servers to assure good uptime and data safety. In test

    environments, you can run all three instances on a single server.3

    Once the config server(s) is/are set up, you can add servers to the sharding cluster.

    Following that the database you want to shard and the collections have to be enabled

    for sharding. In the process of setting up the collection for sharding you also have to

    set up the sharding key. Very much like our previous discussions about sharding,

    choosing keys should be a carefully considered decision in order to insure good

    performance.

    Replication also requires a bit of set up. MongoDB allows for a couple of replicationmodes: master-slave, and a replica set. The major difference is that the replica set is

    more automated. There is still a master, but if a mater fails the replica set choses a

    new master, thus automatically taking care of the failover.4

    4. ConclusionMongoDB allows you to create rich data structures, use sharding and replication all without too

    much difficulty or configuration. Since MongoDB uses BSON / JSON for data storage and display

    it may take a bit to get used to the syntax for MongoDB, especially all the parenthesis and

    brackets. So as you are working through the lesson and homework, be careful of syntax errors.

    3http://docs.mongodb.org/manual/tutorial/deploy-shard-cluster/

    4MongoDB The Definitive Guide, Chodorow Kristina, Dirolf Michael, OReilly, September 2010.

    http://docs.mongodb.org/manual/reference/program/mongod/#bin.mongodhttp://docs.mongodb.org/manual/reference/program/mongod/#bin.mongodhttp://docs.mongodb.org/manual/reference/program/mongod/#bin.mongodhttp://docs.mongodb.org/manual/reference/program/mongod/#bin.mongodhttp://docs.mongodb.org/manual/reference/program/mongod/#bin.mongodhttp://docs.mongodb.org/manual/reference/program/mongod/#bin.mongodhttp://docs.mongodb.org/manual/tutorial/deploy-shard-cluster/http://docs.mongodb.org/manual/tutorial/deploy-shard-cluster/http://docs.mongodb.org/manual/tutorial/deploy-shard-cluster/http://docs.mongodb.org/manual/tutorial/deploy-shard-cluster/http://docs.mongodb.org/manual/reference/program/mongod/#bin.mongodhttp://docs.mongodb.org/manual/reference/program/mongod/#bin.mongod