/
create_index.py
54 lines (42 loc) · 1.74 KB
/
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# db.collection.createIndex() method example in Python
from pymongo import MongoClient, WriteConcern
import re
from datetime import datetime
# Create MongoDB client
mongo_client = MongoClient("mongodb://bigboxuser:bigboxpass@127.0.0.1:27017/")
# Select database and collection
mongo_database = mongo_client.bigboxcode
customer_collection = mongo_database.customer
try:
# Create unique index on email field
command_result = customer_collection.create_index([("email", 1)], unique=True)
print(
"Command: db.customer.createIndex({email: 1}, {unique: true}) | Result:",
command_result,
)
# Check indexes
command_result = customer_collection.index_information()
print("Indexes:", command_result)
# Set index with name
command_result = customer_collection.create_index([("name", 1)], name="my_name_idx")
print(
'Command: db.customer.createIndex({name: 1}, {name: "my_name_idx"}) | Result:',
command_result,
)
# Check indexes
command_result = customer_collection.index_information()
print("Indexes:", command_result)
# Set compound index on address fields with hashed zipcode
command_result = customer_collection.create_index(
[("address.country", 1), ("address.state", 1), ("address.zipcode", "hashed")],
name="address.country_1_address.state_1_address.zipcode_hashed",
)
print(
'Command: Command: db.customer.createIndex({"address.country": 1, "address.state" : 1, "address.zipcode" : "hashed"}) | Result:',
command_result,
)
# Check indexes
command_result = customer_collection.index_information()
print("Indexes:", command_result)
finally:
mongo_client.close()