What are the different indexing types in MongoDB?

Your thoughts?

|

Single Field Index

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

Compound index

db.collection.createIndex({name:1, email:1})

Multikey index (automatically generated if field is an array)

db.collection.createIndex({roles:1})

Text Index

db.collection.createIndex({name:"text"})

Wildcard Index

db.collection.createIndex({"metaData.$**":1})

2dsphere Index

db.collection.createIndex({location:"2dsphere"})

2d Index

db.collection.createIndex({location:"2d"})

Hashed Index

db.collection.createIndex({name:"hashed"})

For examples and when to use what, check out Indexing Types in MongoDB.

|
  • single field
  • compound
  • geoSpatial
  • 2d
  • 2dsphere
  • hashed
  • text

Additionally you have following options...

  • unique
  • sparse
  • partial
  • hidden
|

single and compound

|
  • single field
  • compound
  • hashed
  • geospatial (2d, 2dsphere)