Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
How to create index for MongoDB Collection using Python?
Next article icon

MongoDBis an open-source document-oriented database. MongoDB stores data in the form of key-value pairs and is a NoSQL database program. The term NoSQL means non-relational.

Indexing 

Indexing helps in querying the documents efficiently. Itstores the value of a specific field or set of fields which are ordered by the value of the field as specified in the index. 

PyMongo contains a functioncreate_index()to explicitly create index. By default,_idis the only index present in the collection. This function can accept either a key or a list of (key, direction) pairs.

Syntax: 

create_index(keys, session=None, **kwargs)

Let's look at some examples.

Example 1:

Sample Database:

Python3
frompymongoimportMongoClient# creation of MongoClientclient=MongoClient()# Connect with the portnumber and hostclient=MongoClient("mongodb://localhost:27017/")# Access databasemydatabase=client['GFG']# Access collection of the databasemycollection=mydatabase['College']# Before Creating indexindex_list=sorted(list(mycollection.index_information()))print("Before Creating index")print(index_list)# Creating indexmycollection.create_index("student_id",unique=True)# After Creating indexindex_list=sorted(list(mycollection.index_information()))print("\nAfter Creating index")print(index_list)

Output:

Before Creating index['_id_']After Creating index['_id_', 'student_id_1']
  1. Here, we create an index namedstudent_idusing create_index() method. This results in two indexes in the documents_idandstudent_id.
  2. Usingindex_information() method, we get all the indexes in the collection,

Example 2:

Python3
frompymongoimportMongoClient# creation of MongoClientclient=MongoClient()# Connect with the portnumber and hostclient=MongoClient("mongodb://localhost:27017/")# Access databasemydatabase=client['GFG']# Access collection of the databasemycollection=mydatabase['College']record={'_id':4,"student_id":873,"name":"John","section":"A"}mycollection.insert_one(record)

Output:

DuplicateKeyError                         Traceback (most recent call last)<ipython-input-62-264f0e13db93> in<module>     16 record={'_id':4,"student_id":873,"name":"John","section":"A"}     17---> 18mycollection.insert_one(record)

DuplicateKeyError: E11000 duplicate key error collection: GFG.College index: student_id_1 dup key: { : 873 }

It raises theDuplicateKeyError as there is already a document that exists with the student_id 873 and we are trying to insert another document with the same student_id. This error occurs because we created an index on the field student_id and marked it as unique.


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