mongodb step by step · indexing types single field indexes a single field index only includes data...

13
By B.A.Khivsara Assistant Professor Department of Computer Engineering SNJB’s KBJ COE,Chandwad MongoDB Indexing Step By Step

Upload: others

Post on 18-Jul-2020

27 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: MongoDB Step By Step · Indexing Types Single Field Indexes A single field index only includes data from a single field of the documents in a collection. Compound Indexes A compound

By B.A.Khivsara

Assistant Professor

Department of Computer Engineering

SNJB’s KBJ COE,Chandwad

MongoDB Indexing Step By Step

Page 2: MongoDB Step By Step · Indexing Types Single Field Indexes A single field index only includes data from a single field of the documents in a collection. Compound Indexes A compound

Indexing

Indexes support the efficient execution of queries

in MongoDB.

Page 3: MongoDB Step By Step · Indexing Types Single Field Indexes A single field index only includes data from a single field of the documents in a collection. Compound Indexes A compound

Indexing Types

Single Field Indexes A single field index only includes data from a single field of the documents in a collection. Compound Indexes A compound index includes more than one field of the documents in a collection.

Multikey Indexes A multikey index is an index on an array field, adding an index key for each value in the array.

Geospatial Indexes and Queries Geospatial indexes support location-based searches.

Text Indexes Text indexes support search of string content in documents.

Hashed Index Hashed indexes maintain entries with hashes of the values of the indexed field and are used with sharded clusters to support hashed shard keys.

Page 4: MongoDB Step By Step · Indexing Types Single Field Indexes A single field index only includes data from a single field of the documents in a collection. Compound Indexes A compound

Index Properties

Index Properties The properties you can specify

when building indexes.

1. TTL Indexes The TTL index is used for TTL

collections, which expire data after a period of

time.

2. Unique Indexes A unique index causes

MongoDB to reject all documents that contain a

duplicate value for the indexed field.

3. Sparse Indexes A sparse index does not index

documents that do not have the indexed field.

Page 5: MongoDB Step By Step · Indexing Types Single Field Indexes A single field index only includes data from a single field of the documents in a collection. Compound Indexes A compound

Index Creation

Using CreateIndex

Single: db.stud.createIndex( { zipcode: 1})

Compound: db.stud.createIndex( { dob: 1, zipcode: -1 } )

Unique: db.stud.createIndex( { rollno: 1 }, { unique: true } )

Sparse: db.stud.createIndex( { age: 1 }, { sparse: true } )

Using ensureIndex

Single: db.stud.ensureIndex({“name":1})

Compound: db.stud.ensureIndex ({“address":1,“name":-1})

Note:

1 for Ascending Sorting

-1 for Descending Sorting

Page 6: MongoDB Step By Step · Indexing Types Single Field Indexes A single field index only includes data from a single field of the documents in a collection. Compound Indexes A compound

Index Display

db.collection.getIndexes()

Returns an array that holds a list of documents

that identify and describe the existing indexes on

the collection.

db.collection.totalIndexSize()

Reports the total size used by the indexes on a

collection

db.collection.reIndex()

Rebuilds all existing indexes on a collection.

Page 7: MongoDB Step By Step · Indexing Types Single Field Indexes A single field index only includes data from a single field of the documents in a collection. Compound Indexes A compound

Index Drop

Syntax

db.collection.dropIndexes()- to remove all indexes, except

for the _id index from a collection.

db.collection.dropIndex(index) – to remove specific index

Example

db.stud.dropIndexes()

db.stud.dropIndex( { “name" : 1 } )

Page 8: MongoDB Step By Step · Indexing Types Single Field Indexes A single field index only includes data from a single field of the documents in a collection. Compound Indexes A compound

Indexing and Querying

create an ascending index on the field name for a

collection records:

db.records.createIndex( { name: 1 } )

This index can support an ascending sort on

name :

db.records.find().sort( { name: 1 } )

The index can also support descending sort

db.records.find().sort( { a: -1 } )

Page 9: MongoDB Step By Step · Indexing Types Single Field Indexes A single field index only includes data from a single field of the documents in a collection. Compound Indexes A compound

Indexing and Querying

db.stud.findOne( {rno:2} ), using index {rno:1}

db.stud.find ( {rno:5} ), using index {rno:1}

db.stud.find( {rno:{$in:[2,3]}} ), using index {rno:1}

db.stud.find( {age:{$gt:15}} ), using index {age:1}

db.stud.find( {age :{$gt:2,$lt:5}} ), using index {age :1}

db.stud.count( {age:19} ) using index {age:1}

db.stud.distinct( {branch: “Computer”} ) using index {branch:1}

Page 10: MongoDB Step By Step · Indexing Types Single Field Indexes A single field index only includes data from a single field of the documents in a collection. Compound Indexes A compound

Indexing and Querying

db.stud.find({}, {name:1,age:1}), using index {name:1,age:1}

db.c.find().sort( {name:1,age:1} ), using index {name:1,age:1}

db.stud.update( {age:20}, {age:19} ) using index {age:1}

db.stud.remove( {name: “Jiya”} ) using index {name:1}

Page 11: MongoDB Step By Step · Indexing Types Single Field Indexes A single field index only includes data from a single field of the documents in a collection. Compound Indexes A compound

Indexing with Unique

• Don’t allow {_id:10,x:2} and {_id:11,x:2}

• Don’t allow {_id:12} and {_id:13} (both match {x:null}

db.c.ensureIndex

( {x:1}, {unique:true} )

• Normally index creation fails and the index is removed

• db.ensureIndex( {x:1}, {unique:true,dropDups:true} )

What if duplicates

exist before index is created?

Page 12: MongoDB Step By Step · Indexing Types Single Field Indexes A single field index only includes data from a single field of the documents in a collection. Compound Indexes A compound

Limits and Tradeoffs

Max 40 indexes per collection

Logically equivalent indexes are not prevented

(eg {x:1} and {x:-1})

Indexes can improve speed of queries, but make

inserts slower

More specific indexes {a:1,b:1,c:1} can be more

helpful than less specific indexes {a:1}, but

sorting compound keys may not be as fast as

sorting simple keys

Page 13: MongoDB Step By Step · Indexing Types Single Field Indexes A single field index only includes data from a single field of the documents in a collection. Compound Indexes A compound

References

http://www.docs.mongodb.org/manual/reference