Introduction to MongoDB
1.0 - draft
1.0 - draft
  • MongoDB
  • Getting Started
    • General
      • Database? Collection? Schema?
      • RDBMS? NoSQL?
      • Scale-Up ? Scale - out?
      • JavaScript Object Notation
    • MongoDB
      • 간략한 역사,
    • Manipulating Database
      • MongoShell
      • PyMongo
  • CRUD
    • General
      • Explain()
      • mongoimport
    • Create
      • Mongo Shell
      • PyMongo
      • Exercises
    • Read
      • Mongo Shell
      • PyMongo
      • Exercises
    • Update
      • Mongo Shell
      • PyMongo
      • Exercises
    • Delete
      • PyMongo
      • Exercises
    • Challenging Exercises
      • Exercise 1
  • Index with Geospatial & Text
    • Basic Index
      • Syntax
      • Exercises
    • Geospatial Index
      • Syntax
      • Exercises
    • Text Index
      • Syntax
      • Exercises
    • Challenge
  • Aggregate framework
    • Aggregate Framework
      • Basic
      • Exercises
  • Distributed
    • Replication
      • Setup
        • Configuration File
        • Running Servers
        • Initiation
        • Exercises
      • Concerns
        • Read Concern
        • Write Concern
        • Exercises
    • Sharding
      • Getting Started
      • Setup
  • MongoDB 4.0 Features
    • Multi-Document Transaction
      • Transaction?
      • Basic
      • Exercises
  • Administration
    • Untitled
Powered by GitBook
On this page
  • 기본 구문
  • Create Index
  • Show Index
  • Drop Index
  • Compound Index
  • Indexing on Embedded Document
  • Indexing Option
  • Unique Indexes
  • Sparse Indexes
  • Unique & Sparse
  • Hint

Was this helpful?

  1. Index with Geospatial & Text
  2. Basic Index

Syntax

기본 구문

Create Index

  • create_index([(‘index_name’, order)])

    • Creates an index on this collection.

      my_collection.create_index([(‘address’, 1)])

Show Index

  • index_information()

    • Get information on collections’s indexes.

      my_collection.index_information()

Drop Index

  • drop_index ([(‘index_name’, order)])

    • Drops the specified index on this collection.

    • To drop all indexes on this collection, use drop_indexes().

      my_collection.drop_index([(‘address’, 1)])

Compound Index

  • Create indexes on more than one field.

  • my_collection.create_index([
    (‘name’, pymongo.DESCENDING),
    (‘address’, pymongo.ASCENDING)])

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

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

Indexing on Embedded Document

  • Embedded document와 Array에 인덱스를 사용하기 위해서는 아래와 같은 구문으로 사용하면 된다. 개념은 똑같기 때문에 이해하는 것에는 무리없을 것으로 보인다.

  • Indexes can be created on keys in embedded documents.

    collection.create_index([('parent.child', pymongo.ASCENDING)])
  • To use the index to search for specific array elements efficiently.

    collection.create_index([('items.name', pymongo.DESCENDING)])

Indexing Option

Unique Indexes

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

    collection.create_index([('key_1', 1)], unique=True)

Sparse Indexes

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

    collection.create_index([('key_2', 1)], sparse=True)

Unique & Sparse

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

    collection.create_index([(key_3', 1)], unique=True, sparse=True)

Hint

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

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

Last updated 5 years ago

Was this helpful?