Indexes

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Creación de un índice de campo único

Create a Single Field Index


db.customers.createIndex({

birthdate: 1

})

Create a Unique Single Field Index


db.customers.createIndex({

email: 1

},

unique:true

})

View the Indexes used in a Collection


db.customers.getIndexes()

Check if an index is being used on a query


db.customers.explain().find({

birthdate: {

$gt:ISODate("1995-08-01")

})
db.customers.explain().find({

birthdate: {

$gt:ISODate("1995-08-01")

}).sort({

email:1

})

Understanding Multikey Indexes


Create a Single field Multikey Index
db.customers.createIndex({

accounts: 1

})

db.customers.explain().find({

accounts: 627788

})

Working with Compound Indexes


Create a Compound Index
db.customers.createIndex({

active:1,

birthdate:-1,

name:1
})

Order of Fields in a Compound Index


db.customers.find({

birthdate: {

$gte:ISODate("1977-01-01")

},

active:true

}).sort({

birthdate:-1,

name:1

})

db.customers.createIndex({

active:1,

birthdate:-1,

name:1

})

Eliminación de un índice
Delete an Index
db.customers.dropIndex(

'active_1_birthdate_-1_name_1'

)
db.customers.dropIndex({

active:1,

birthdate:-1,

name:1

})

Delete Indexes
db.customers.dropIndexes()

db.collection.dropIndexes([

'index1name', 'index2name', 'index3name'

])

You might also like