Getting started with mongodb By Reeshu Patel

GETTING STARTED WITH MONGODB

MongoDB Architecture MongoDB is the suported NoSQL database, empowering businesses to be more agile and scalable.,and its supported to cluster architecture and improve customer experience, accelerate time to market and reduce costs. MongoDB is a general purpose, open-source database. It features: Document data model with dynamic schemas Full, flexible index support and rich queries Auto-Sharding for horizontal scalability Built-in replication for high availability Text search Advanced security Aggregation Framework and MapReduce Large media storage with GridFS

Instead of mongo db storing data in rows and columns as one would with a relational database, mostaly MongoDB stores a binary form of JSON GETTING STARTED WITH MONGODB

documents (BSON). Relational databases impose flat, rigid schemas across many tables.

NoSQL Introduction Mongodb article touches upon various aspects of database and where database hits the roadblock in an enterprise world. This article also talks about how we can overcome this road blocks by relaxing few key aspects of RDBMS. It also talks about how a NoSQL based solution fits into this and as a popular

Introduction to MongoDB MongoDB is a powerful, flexible, and scalable general-purpose database. It combines the ability to scale out with features such as secondary indexes, range queries, sorting, aggregations, and geospatial indexes. This chapter covers the major design decisions that made MongoDB what it is

Ease of Use mongodb MongoDB is a document-oriented database, not a relational one. The primary reason for moving away from the relational model is to make scaling out easier, but there are some other advantages as well. A document-oriented database replaces the concept of a “row” with a more flexible model, the “document.” By allowing embedded documents and arrays, the document oriented approach makes it possible to represent complex hierarchical relationships with a single record. This fits naturally into the way developers in modern object oriented languages think about their data.

Easy Scaling of mongodb GETTING STARTED WITH MONGODB

Data set sizes for applications are growing at an incredible pace. Increases in available bandwidth and cheap storage have created an environment where even small-scale applications need to store more data than many databases were meant to handle. A terabyte of data, once an unheard-of amount of information, is now commonplace. As the amount of data that developers need to store grows, developers face a difficult decision: how should they scale their databases? Scaling a database comes down to the choice between scaling up (getting a bigger machine) or scaling out (partitioning data across more machines). Scaling up is often the path of least resistance, but it has draw backs: large machines are often very expensive, and eventually a physical limit is reached where a more powerful machine cannot be purchased at any cost. The alternative is to scale out: to add storage space or increase performance, buy another commodity server and add it to your cluster. This is both cheaper and more scalable; however, it is more difficult to administer a thousand machines than it is to care for one.

Features of mongodb MongoDB is intended to be a general-purpose database, so aside from creating, reading, updating, and deleting data, it provides an ever-growing list of unique features.all these features given bleow. 1:-Indexing 2:-Aggregation 3:-Special collection types 4:-File storage MongoDB supports an easy-to-use protocol for storing large files and file metadata GETTING STARTED WITH MONGODB

MongoDB Installation MongoDB is powerful but easy to get started with. In this chapter we’ll introduce some of the basic concepts of MongoDB: A document is the basic unit of data for MongoDB and is roughly equivalent to a row in a relational database management system (but much more expressive). 1:-Similarly, a collection can be thought of as a table with a dynamic schema. 2:-A single instance of MongoDB can host multiple independent databases, each of which can have its own collections.

Install MongoDB on Ubuntu Getting started with MongoDB is easy 1:-Download Mongo 2:-Install the Software 3:-Create a Directory for Data 4:-Installation Layout 5:-Running Mongo 6:-Learn About MongoDB

Step1:Download Mongo MongoDB is available either in pre-built distribution for Linux, OS X and Windows, or via source You can find the available pre-built distributions of MongoDB on the download blow link: http://downloads.mongodb.org/linux/mongodb-linux-i686-1.2.2.tgz See Version Numbers The linux legacy-static builds are only recommended for older systems. If you try to run and get a floating point exception, try the legacy-static builds. Otherwise you should use the regular ones. GETTING STARTED WITH MONGODB

Adding a dedicated mongodb system user We will use a dedicated mongodb user account for running mongodb. While that’s not required it is recommended because it helps to separate the mongodb installation from other software applications and user accounts running on the same machine (think: security, permissions, backups, etc). $ sudo adduser mongodb

Installation Download mongodb from the Download Mirrors and extract the contents of the mongodb package to a location of your choice. I picked /usr/local/mongodb . Make sure to change the owner of all the files to the mongodb user for example: 1 $ cd /usr/local 2 $ sudo /mongodb-linux-i686-static-1.2.2.tgz 3 $ sudo mv /mongodb-linux-i686-static-1.2.2.tgz mongodb 4 $ sudo chown -R mongodb mongodb

Step2:Update $HOME/.bashrc Add the following lines to the end of the $HOME/.bashrc file of user mongodb. If you use a shell other than bash, you should of course update its appropriate configuration files instead of .bashrc. export MONGODB_HOME=/usr/local/mongodb export PATH=$PATH:$MONGODB_HOME/bin

Install the Software

Installation of MongoDB is easy. Once you've downloaded the software and unpacked the distribution into the MongoDB home directory, you'll need to create a directory for MongoDB to store its data files. GETTING STARTED WITH MONGODB

Now lets install mongodb. by command sudo apt-key adv --keyserver keyserver.ubuntu.com --recv 7F0CEB10 sudo gedit /etc/apt/sources.list and put this file deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10ge sudo apt-get update sudo apt-get install mongodb-10gen

Step3:Create Data Directory By default, MongoDB will store data in /data/db on Unix-like systems (e.g. Linux and OS X), and in c:\data\db in Windows. MongoDB will not create these directories, so please create them. They'll need to have read, write and directory creation permissions for Mongo to perform all of its usual operations. You can also specify a different directory with the --dbpath flag. we will creat dir from root: $ sudo mkdir /data/db $ sudo chown `id -u` /data/db $ sudo chown mongodb /data/db $ sudo chmod 750 /data/db

Step4:Now we another directory for store log $sudo mkdir /var/log/mongodb $sudo chown mongodb /var/lib/mongodb/ GETTING STARTED WITH MONGODB

Step5:Now lets edit /etc/mongodb.conf and change the location of the database. Near the top change dbpath so it looks like this: root@boss[bin]#vi /etc/mongodb.conf #dbpath=/var/lib/mongodb Dbpath=/data/db

I also like to change my oplogSize to something larger than the default so if a secondary instance is down I have longer to bring it back up before it becomes too stale to re-sync. I also recommend turning on journaling to prevent data corruption. oplogSize = 10000 replSet = myReplicaSet journal = true

Important

You are useing cluster environment then Used

If you’re using a hostname in the replica set configuration instead of the IP address, you need to configure that in /etc/hostname and /etc/hosts /etc/hostname: database1 /etc/hosts: 127.0.0.1 localhost

database1 database1.mydomain.com

192.168.15.102

localhost.localdomain

database1 database1.mydomain.com

(where 192.168.15.102 is this machine’s IP address that you use in the replica set config. Usually the elastic IP.) GETTING STARTED WITH MONGODB

After changing hostname information you’ll need to restart mongodb server

Step6:Now start the mongoDB form root root@boss[bin]#/etc/init.d/mongodb strat if You want to check log : root@boss[bin]#tail -f /var/log/mongodb/mongodb.log

Get the owner & access details root@boss[bin]#ls -l /data/db total 163848 drwxr-xr-x 2 rbradfor admin 68 Jun 8 16:07 _tmp -rwxr-xr-x 1 rbradfor admin 6 Jun 8 16:06 mongod.lock -rw------- 1 rbradfor admin 67108864 Jun 8 16:07 test.0 -rw------- 1 rbradfor admin 16777216 Jun 8 16:07 test.ns

Stop the mongoDB server root@boss[bin]#/etc/init.d/mongodb stop Stopping database: mongodb.

Now we restart the mongodb server: root@boss[bin]#/etc/init.d/mongodb strat

Running Mongo Running the database is as easy as starting the server. On Linux, and assuming you are in the MongoDB home directory, just type GETTING STARTED WITH MONGODB

$ bin/mongod $ ./mongodb-osx-i386-1.4.3/bin/mongod which will start the database. For further information on running MongoDB, ********************************************************************** $ sudo chown `id -u` /data/db $ ./mongodb-osx-i386-1.4.3/bin/mongod Tue Jun 8 16:06:37 Mongo DB : starting : pid = 78284 port = 27017 dbpath = /data/db/ master = 0 slave = 0 32-bit NOTE: when using MongoDB 32 bit, you are limited to about 2 gigabytes of data see http://blog.mongodb.org/post/137788967/32-bit-limitations for more. Tue Jun 8 16:06:37 db version v1.4.3, pdfile version 4.5 Tue Jun 8 16:06:37 git version: 47ffbdfd53f46edeb6ff54bbb734783db7abc8ca Tue Jun 8 16:06:37 sys info: Darwin broadway.local 9.8.0 Darwin Kernel Version 9.8.0: Wed Jul 15 16:55:01 PDT 2009; root:xnu1228.15.4~1/RELEASE_I386 i386 BOOST_LIB_VERSION=1_40 Tue Jun 8 16:06:37 waiting for connections on port 27017 Tue Jun 8 16:06:37 web admin interface listening on port 28017 ********************************************************************** You are then ready to rock and roll. Use the exit command to exit the mongo interactive shell.open next tab and to the mongo shell. To run the shell : bin/mongo ********************************************************************** $ ./mongodb-osx-i386-1.4.3/bin/mongo > db.foo.save ({a:2}); > db.foo.find(); GETTING STARTED WITH MONGODB

{ "_id" : ObjectId("4c0ea308f5ea2f7148a33b9f"), "a" : 2 } > exit bye ********************************************************************** The mongod output of the first save of data into the “foo” collection shows a little of how mongodb operations. There is a lazy instantiation of “test” database when first required. There is no need to first create this, or use this as with MySQL. ********************************************************************** Tue Jun 8 16:07:36 allocating new datafile /data/db/test.ns, filling with zeroes... Tue Jun 8 16:07:36 done allocating datafile /data/db/test.ns, size: 16MB, took 0.05 secs Tue Jun 8 16:07:37 allocating new datafile /data/db/test.0, filling with zeroes... Tue Jun 8 16:07:38 done allocating datafile /data/db/test.0, size: 64MB, took 1.282 secs Tue Jun 8 16:07:40 building new index on { _id: 1 } for test.foo Tue Jun 8 16:07:40 Buildindex test.foo idxNo:0 { name: "_id_", ns: "test.foo", key: { _id: 1 } } Tue Jun 8 16:07:40 done for 0 records 0.018secs Tue Jun 8 16:07:40 insert test.foo 3181ms ********************************************************************** $ ls -l /data/db total 163848 drwxr-xr-x 2 rbradfor admin 68 Jun 8 16:07 _tmp -rwxr-xr-x 1 rbradfor admin 6 Jun 8 16:06 mongod.lock -rw------- 1 rbradfor admin 67108864 Jun 8 16:07 test.0 -rw------- 1 rbradfor admin 16777216 Jun 8 16:07 test.ns One observation is the output of the mongod is more a trace output. I have yet to see any information about a more appropriately formatted error log. GETTING STARTED WITH MONGODB

P.S:if You want to worked with mongodb user.You can do. Now we Go to the : mongodb@reeeshu:/usr/local/mongodb/bin$ ./mongo MongoDB shell version: 2.4.6 connecting to: test Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user Server has startup warnings: Thu Aug 29 14:25:31.649 [initandlisten] Thu Aug 29 14:25:31.649 [initandlisten] ** NOTE: This is a 32 bit MongoDB binary. Thu Aug 29 14:25:31.649 [initandlisten] ** than 2GB of data (or less with --journal).

32 bit builds are limited to less

Thu Aug 29 14:25:31.649 [initandlisten] ** to off for 32 bit and is currently off.

Note that journaling defaults

Thu Aug 29 14:25:31.649 [initandlisten] ** http://dochub.mongodb.org/core/32bit

See

Thu Aug 29 14:25:31.649 [initandlisten] > exit bye GETTING STARTED WITH MONGODB

mongodb@reeeshu:/usr/local/mongodb/bin$ Now we can see screen short here:

kill current mongod process Find mongo processes ps -ef | grep mongo you'll get mongod PID mongodb PID 1 0 06:26 ? /etc/mongodb.conf

00:00:00 /usr/bin/mongod --config

Stop current mongod process kill -9 PID killall -9 mongod Check on performance and statistics on browser 1:-View contents and status of server with your browser http://localhost:28017/

GETTING STARTED WITH MONGODB

Fig 1.2 mongodb on server 2:-View the log file

Fig 1.3 mongodb log file

Here are some simple admin steps to start/stop MongoDb server # Install MongoDB GETTING STARTED WITH MONGODB

mkdir /data/lib

# Start Mongod server .../bin/mongod # data stored in /data/db

# Start the command shell .../bin/mongo > show dbs > show collections

# Remove collection > db.person.drop()

# Stop the Mongod server from shell > use admin > db.shutdownServer()

Databases In addition to grouping documents by collection, MongoDB groups collections into databases. A single server of MongoDB can host several databases, each grouping together zero or more collections. A database has its own permissions, and each database is stored in separate files on disk. A good rule of thumb is to store all data for a single application in the same database. Separate databases are useful when storing data for several application or users on the same MongoDB server. GETTING STARTED WITH MONGODB

• The empty string ("") is not a valid database name. • Database names are case-sensitive, even on non-case-sensitive filesystems. To keep things simple, try to just use lowercase characters. • Database names are limited to a maximum of 64 bytes. One thing to remember about database names is that they will actually end up as files on your filesystem. This explains why many of the previous restrictions exist in the first place There are also several reserved database names, which you can access but which have special semantics. These are as follows: 1:-admin 2:-local 3:-config By concatenating a database name with a collection in that database you can get a fully qualified collection name called a namespace. For instance, if you are using the blog.posts collection in the cms database, the namespace of that collection would be cms.blog.posts. Namespaces are limited to 121 bytes in length and, in practice, should be fewer than 100 bytes long. For more on namespaces and the internal representation of collections in MongoDB . Now we can insert create and save data in a table with the help of following documents looks as below, post={ author:“Hergé”, date:new Date(), text:“Destination Moon”, tags:[“comic”,“adventure”] } GETTING STARTED WITH MONGODB

> db.post.save(post) ----------->db.posts.find() { _id:ObjectId(" 4c4ba5c0672c685e5e8aabf3"), author:"Hergé", date:"Sat Jul 24 2010 19:47:11 GMT-0700(PDT)", text:"Destination Moon", tags:["comic","adventure"] } In MongoDb, atomicity is guaranteed within a document. If you have to achieve atomicity outside of the document, it has to be managed at the application level. Below is an example,

SQL to MongoDB Mapping Chart In addition to the charts that follow, you might want to consider the Frequently Asked Questions section for a selection of common questions about MongoDB. Executables The following table presents the MySQL/Oracle executables and the corresponding MongoDB executables.

GETTING STARTED WITH MONGODB

Database Server Database Client

MySQL/Orac MongoDB le mysqld/oracle mongod mysql/sqlplus mongo

Terminology and Concepts

The following table presents the various SQL terminology and concepts and the corresponding MongoDB terminology and concepts. SQL Terms/Concepts database table row column index table joins primary key

MongoDB Terms/Concepts database collection document or BSON document field index embedded documents and linking primary key

Specify any unique column or In MongoDB, the primary key is column combination as primary key. automatically set to the _id field. aggregation (e.g. group by)

aggregation pipeline See the SQL to Aggregation Mapping Chart.

Examples

The following table presents the various SQL statements and the corresponding MongoDB statements. The examples in the table assume the following conditions: The SQL examples assume a table named users. The MongoDB examples assume a collection named users that contain documents of the following prototype: { GETTING STARTED WITH MONGODB

}

_id: ObjectID("509a8fb2f3f4948bd2f983a0"), user_id: "abc123", age: 55, status: 'A'

Create and Alter The following table presents the various SQL statements related to table-level actions and the corresponding MongoDB statements. SQL Schema Statements CREATE TABLE users ( id MEDIUMIN T NOT NULL AUTO_INCR EMENT, user_id Varchar(30), age Number, status char(1), PRIMARY KEY (id) ) ALTER TABLE users ADD join_date

MongoDB Schema Statements Implicitly created on first insert() operation. The primary key _id is automatically added if _id field is not specified.

Reference See insert() and db.createCollect ion() for more information.

db.users.insert( { user_id: "sdhk123", age: 55, status: "A" }) However, you can also explicitly create a collection: db.createCollection("user s")

Collections do not describe or enforce the structure of its documents; i.e. there is

See the Data Modeling Considera GETTING STARTED WITH MONGODB

DATETIME

no structural alteration at the collection level. However, at the document level, update() operations can add fields to existing documents using the $set operator. db.users.update( { }, { $set: { join_date: new Date() } }, { multi: true } )

ALTER TABLE users DROP COLUMN join_date

Collections do not describe or enforce the structure of its documents; i.e. there is no structural alteration at the collection level. However, at the document level, update() operations can remove fields from documents using the $unset operator. db.users.update( { }, { $unset: { join_date: "" } }, { multi: true } )

CREATE INDEX

db.users.ensureIndex( {

tions for MongoD B Applicati ons, update(), and $set for more informati on on changing the structure of document s in a collection. See Data Modeling Considera tions for MongoD B Applicati ons, update(), and $unset for more informati on on changing the structure of document s in a collection. See ensureInd

GETTING STARTED WITH MONGODB

idx_user_id_a sc ON users(user_id)

user_id: 1 } )

CREATE INDEX

db.users.ensureIndex( { user_id: 1, age: -1 } )

idx_user_id_a sc_age_desc ON users(user_id, age DESC) DROP TABLE users

db.users.drop()

ex() and indexes for more informati on. See ensureInd ex() and indexes for more informati on. See drop() for more information.

Insert The following table presents the various SQL statements related to inserting records into tables and the corresponding MongoDB statements SQL INSERT Statements INSERT INTO users(user_id, age, status) VALUES ("bcd001", 45, "A")

MongoDB insert() Reference Statements db.users.insert( { See insert() for more user_id: "bcd001", information. age: 45, status: "A" })

Inserts a document or an array of documents into a collection.

GETTING STARTED WITH MONGODB

db.collection.insert()

Examples

The following example inserts a document into the :The users Example Collection db.users.insert( { name: { first: 'John', last: 'McCarthy' }, birth: new Date('Sep 04, 1927'), death: new Date('Dec 24, 2011'), contribs: [ 'Lisp', 'Artificial Intelligence', 'ALGOL' ], awards: [ { award: 'Turing Award', year: 1971, by: 'ACM' }, { award: 'Kyoto Prize', year: 1988, by: 'Inamori Foundation' }, { award: 'National Medal of Science', year: 1990, by: 'National Science Foundation' } ] } ) To verify the inserted document, query the users collection: db.users.find( { name: { first: 'John', last: 'McCarthy' } } )

The returned document includes an _id field with the generated ObjectId value: ********************************************************************** GETTING STARTED WITH MONGODB

{

}

"_id" : ObjectId("50a1880488d113a4ae94a94a"), "name" : { "first" : "John", "last" : "McCarthy" }, "birth" : ISODate("1927-09-04T04:00:00Z"), "death" : ISODate("2011-12-24T05:00:00Z"), "contribs" : [ "Lisp", "Artificial Intelligence", "ALGOL" ], "awards" : [ { "award" : "Turing Award", "year" : 1971, "by" : "ACM" }, { "award" : "Kyoto Prize", "year" :1988,s "by" : "Inamori Foundation" }, { "award" : "National Medal of Science", "year" : 1990, "by" : "National Science Foundation" } ]

********************************************************************** P.S::Most MongoDB driver clients will include the _id field and generate an ObjectId before sending the insert operation to MongoDB; however, if the client sends a document without an _id field, the mongod will add the _id field and generate the ObjectId.

Insert a Document Specifying an _id Field In the following examples, the documents passed to the insert() method includes the _id field. The value of _id must be unique within the collection to avoid duplicate key error.

Users Collection method GETTING STARTED WITH MONGODB

This example inserts into the users collection a document that includes an _id field. The _id value of 10 must be a unique value for the _id field in the Users collection. If the value were not unique, the insert would fail: db.users.insert( { _id: 10, item: "box", qty: 20 } ) The insert operation creates the following document in the users collection: { "_id" : 10, "item" : "box", "qty" : 20 }

users Collection This example inserts into the users collection a document that includes an _id field. The _id value of 1 must be a unique value for the _id field in the users collection. Otherwise, if the value were not unique, the insert would fail: db.users.insert( { _id: 1, name: { first: 'John', last: 'Backus' }, birth: new Date('Dec 03, 1924'), death: new Date('Mar 17, 2007'), contribs: [ 'Fortran', 'ALGOL', 'Backus-Naur Form', 'FP' ], awards: [ { award: 'W.W. McDowell Award', year: 1967, by: 'IEEE Computer Society' }, { award: 'National Medal of Science', year: 1975, by: 'National Science Foundation' }, { award: 'Turing Award', year: 1977, by: 'ACM' }, GETTING STARTED WITH MONGODB

{

)

}

]

}

award: 'Draper Prize', year: 1993, by: 'National Academy of Engineering'

To confirm the insert, query the bios collection: db.users.find( { _id: 1 } ) The insert operation created the following document in the users collection:

Delete Records method The following table presents the various SQL statements related to deleting records from tables and the corresponding MongoDB statements. SQL Delete MongoDB remove() Statements Reference Statements DELETE db.users.remove( { status: "D" } See remove() for ) FROM users more information. WHERE status = "D" DELETE FROM users

db.users.remove( )

See remove() for more information.

GETTING STARTED WITH MONGODB

Indexing Concepts of mongodb Introduction to Indexing Indexes provide high performance read operations for frequently used queries. Indexes are particularly useful where the total size of the documents exceeds the amount of available RAM.

Index Concepts A database index is similar to a book’s index. Instead of looking through the whole book, the database takes a shortcut and just looks at an ordered list that points to the content, which allows it to query orders of magnitude faster. For example, let’s create a collection with 1 million documents in it (or 10 million or 100 million, if you have the patience): > for (i=0; i db.users.find({username: "user101"}).explain() { "cursor" : "BasicCursor", "nscanned" : 1000000, "nscannedObjects" : 1000000, "n" : 1, "millis" : 721, "nYields" : 0, "nChunkSkips" : 0, "isMultiKey" : false, "indexOnly" : false, "indexBounds" : { } }

Create an Index Indexes allow MongoDB to process and fulfill queries quickly by creating small and efficient representations of the documents in a collaction Try creating an index on the username field: > db.users.ensureIndex({"username" : 1}) Depending on your machine and how large you made the collection, creating an index may take a few minutes Once the index build is complete, try repeating the original query: > db.users.find({"username" : "user101"}).explain() This explain() output is more complex, but continue to ignore all the fields other Getting started with mongodb 27

than "n", "nscanned", and "millis" for now. > db.users.find({username: "user999999"}).explain().millis 0 As you can see, an index can make a dramatic difference in query times . Fig:1:C reate Index.

Create a Compound Index An index keeps all of its values in a sorted order so it makes sorting documents by the indexed key much faster. However, an index can only help with sorting if it is a prefix of the sort. For example, the index on "username" wouldn’t help much for this sort: > db.users.find().sort({"age" : 1, "username" : 1}) you could make an index on "age" and "username": > db.users.ensureIndex({"age" : 1, "username" : 1}) This is called a compound index and is useful if your query has multiple sort directions or multiple keys in the criteria. You can diagnose how MongoDB defaults to processing db.users.find({"age" : {"$gte" : 21, "$lte" : 30}}).sort({"username" : 1}) by using explain():

28

Getting started with mongodb

> db.users.find({"age" : {"$gte" : 21, "$lte" : 30}}). ... sort({"username" : 1}). ... explain()

Ranges of index Compound indexes can help MongoDB execute more effectively queries with multiple clauses. When designing an index with multiple fields, put fields that will be used in exact matches first (e.g., "x" : "foo") and ranges last (e.g., "y": {"$gt" : 3, "$lt" : 5}).

For example, suppose we were querying for a specific age and a range of

usernames using an {"age" : 1, "username" : 1} index. We would get fairly exact index bounds: > db.users.find({"age" : 47, ... "username" : {"$gt" : "user5", "$lt" : "user8"}}).explain() Next db.users.find({"age" : 47, ... "username" : {"$gt" : "user5", "$lt" : "user8"}}).explain() Output for frist **************************************************************************** Getting started with mongodb 29

{ "cursor" : "BtreeCursor age_1_username_1", "n" : 2788, "nscanned" : 2788, ..., "indexBounds" : { "age" : [ [ 47, 47 ] ], "username" : [ [ "user5", "user8" ] ] }, ... } ****************************************************************************

30

Getting started with mongodb

Types of Indexes There are a few index options you can specify when building an index that change the way the index behaves. The most common variations are described in the following sections, and more advanced or special-case options are described in the section.

1:-Create a Unique Index nique indexes guarantee that each value will appear at most once in the index. For example, if you want to make sure no two documents can have the same value in the "username" key, you can create a unique index: > db.users.ensureIndex({"username" : 1}, {"unique" : true}) For example, suppose that we try to insert the following documents on the collection above: > db.users.insert({username: "bob"}) > db.users.insert({username: "bob"}) Getting started with mongodb 31

E11000 duplicate key error index: test.users.$username_1 dup key: { : "bob" } If you check the collection, you’ll see that only the first "bob" was stored.

2:-Compound unique indexes You can also create a compound unique index. If you do this, individual keys can have the same values, but the combination of values across all keys in an index entry can appear in the index at most once. For example, if we had a unique index on {"username" : 1, "age" : 1}, the following inserts would be legal: > db.users.insert({"username" : "amit"}) > db.users.insert({"username" : "amit", "age" : 25}) > db.users.insert({"username" : "amit", "age" : 25}) However, attempting to insert a second copy of any of these documents would cause a duplicate key exception.

Dropping duplicates > db.users.ensureIndex({"age" : 1}, {"unique" : true}) E11000 duplicate key error index: test.users.$age_1 dup key: { : 12 } The "dropDups" option will save the first document found and remove any subsequent documents with duplicate values: > db.people.ensureIndex({"username" : 1}, {"unique" : true, "dropDups" : true})

Sparse Indexes To create a sparse index, include the sparse option. For example, if providing an email address was optional but, if provided, should be unique, we could do: > db.ensureIndex({"email" : 1}, {"unique" : true, "sparse" : true}) 32

Getting started with mongodb

Sparse indexes do not necessarily have to be unique. To make a non-unique sparse index, simply do not include the unique option. One thing to be aware of is that the same query can return different results depending on whether or not it uses the sparse index. For example, suppose we had a collection where most of the documents had "x" fields, but one does not: >db.foo.find() {"_id" : 0 } {"_id" : 1, "x" : 1 } {"_id" : 2, "x" : 2 } {"_id" : 3, "x" : 3 } When we do a query on "x", it will return all matching documents: >db.foo.find({"x" : {"$ne" : 2}}) {"_id" : 0 } {"_id" : 1, "x" : 1 } {"_id" : 3, "x" : 3 } Getting started with mongodb 33

If we create a sparse index on "x", the "_id" : 0 document won’t be included in the index. So now if we query on "x", MongoDB will use the index and not return the {"_id" : 0} document: > db.foo.find({"x" : {"$ne" : 2}}) { "_id" : 1, "x" : 1 } { "_id" : 3, "x" : 3 } You can use hint() to force it to do a table scan if you need documents with missing fields.

Index Administration All of the information about a database’s indexes is stored in the system.indexes collection. This is a reserved collection, so you cannot modify its documents or remove documents from it. You can manipulate it only through ensureIndex and the dropIn dexes database command. When you create an index, you can see its meta information in system.indexes. You can also run db.collectionName.getIndexes() to see all index information about a given collection: **************************************************************************** > db.foo.getIndexes() [{ "v" : 1,"key" : {"_id" : 1},"ns" : "test.foo","name" : "_id_"},{"v" : 1,"key" : { 34

Getting started with mongodb

"y" : 1},"ns" : "test.foo","name" : "y_1"}, {"v" : 1 "key" : {"x" : "y" : 1"ns" : "test.foo","name" : "x_1_y_1"}] ****************************************************************************

Fig: Screen short of Creating Index.

2D Indexes For non-spherical maps (video game maps, time series data, etc.) you can use a "2d" index, instead of "2dsphere": > db.hyrule.ensureIndex({"tile" : "2d"}) "2d" indexes assume a perfectly flat surface, instead of a sphere. Thus, "2d" indexes should not be used with spheres unless you don’t mind massive distortion around the poles.

Getting started with mongodb 35

36

Getting started with mongodb

Getting started with mongodb 37