Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Get all the information of a Collection's indexes using PyMongo
Next article icon

Creating indexes inMongoDB improves query performance, especially on large datasets. PyMongo, the official MongoDB driver for Python, provides thecreate_index() method to define indexes on specific fields. By default, MongoDB indexes the _idfield, but adding custom indexes is important for optimizing complex queries.

Syntax

collection.create_index([(field, direction)], **options)

Parameters:

Returns: This method returns the name of the created index as a string.

Examples

Example 1: Create a single field index

Python
frompymongoimportMongoClientc=MongoClient("mongodb://localhost:27017/")db=c["mydb"]col=db["students"]col.drop()col.insert_many([{"name":"Ava","age":22},{"name":"Liam","age":24},{"name":"Emma","age":20}])idx_name=col.create_index([("name",1)])print(idx_name)

Output

name_1

Explanation: create_index([("name", 1)])creates an ascending index on the name field to speed up queries and sorting and returns "name_1", the default name based on the field and sort order.

Example 2: Create a compound index (multiple fields)

Python
frompymongoimportMongoClientc=MongoClient("mongodb://localhost:27017/")db=c["mydb"]col=db["students"]col.drop()col.insert_many([{"name":"Ava","city":"New York"},{"name":"Liam","city":"Chicago"},{"name":"Emma","city":"Boston"}])idx_name=col.create_index([("name",1),("city",-1)])print(idx_name)

Output

name_1_city_-1

Explanation:This creates a compound index on name (ascending) and city (descending). It helps optimize queries that filter or sort on both fields and returns the default index name "name_1_city_-1".

Example 3: Create a unique index

Python
frompymongoimportMongoClientc=MongoClient("mongodb://localhost:27017/")db=c["mydb"]col=db["students"]col.drop()col.insert_many([{"email":"ava@example.com"},{"email":"liam@example.com"},{"email":"emma@example.com"}])idx_name=col.create_index([("email",1)],unique=True)print(idx_name)

Output

email_1

Explanation:Creates an ascending unique index on the email field to prevent duplicates. Inserting a duplicate triggers a DuplicateKeyError.

Related article


Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp