■ Collection 클래스의 create_index 메소드를 사용해 인덱스를 생성하는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
from pymongo import MongoClient, ASCENDING databaseURL = "mongodb://localhost:27017/" mongoClient = MongoClient(databaseURL) testDatabase = mongoClient["testdb"] userCollection = testDatabase["user"] userCollection.delete_many({}) userList = [] for i in range(1, 6): user = { "email" : f"user{i}@sample.com", "password" : f"password{i}" } userList.append(user) userCollection.insert_many(userList) indexName = testDatabase.userCollection.create_index([("email", ASCENDING)], unique = True) print(indexName) # 인덱스명 |
▶ requirements.txt
1 2 3 4 |
dnspython==2.6.1 pymongo==4.7.2 |