Index

Functions for Index

  • Creates an index on this collection.

create_index([(‘index_name’, order)]) 
e.g.) my_collection.create_index([(‘address’, 1)]) 
  • Get information on collections’s indexes

index_information()
e.g.) my_collection.index_information()
  • Drops the specified index on this collection

drop_index ([(‘index_name’, order)])
my_collection.drop_index([(‘address’, 1)]) 
  • To drop all indexes on this collection, use drop_indexes()

Compound Index

  • Create indexes on more than one field.

my_collection.create_index([
(‘name’, pymongo.DESCENDING),
(‘address’, pymongo.ASCENDING)])
e.g.)
my_collection.create_index([(‘name’, -1), (‘address’, 1)])

PyMongo에서 가독성을 위해 값을 제공하고 있다. 아래는 같은 뜻으로 이해하면 되겠다.

pymongo.ASCENDING = 1, pymongo.DESCENDING = -1

Indexing on Embedded Document

  • Indexes can be created on keys in embedded documents.

  • Indexing to Array

Unique & Sparse Index

  • Unique indexes guarantee that each value will appear at most once in the index

  • Sparse indexes only contain entries for documents that have the indexed field skipping documents without it.

  • Unique & Sparse

Hint

  • To override MongoDB’s default index selection and query optimization process.

collection.find().sort([('key_4', 1)]).hint([('key_4', 1)])

Geospatial Index

  • Type

    • pymongo.GEO2D ==> 2d

    • pymongo.GEOSPHERE ==> 2dsphere

  • Example

Text Index

  • Create a text index on one field

  • Create text indexes on multiple fields

  • Specify the weight of an indexed

Last updated

Was this helpful?