This program is a bit more complex than some. It tests the performance gain from creating an index on a field in a collection.
from pymongo import MongoClient
import random
import time
# Generate 20000 random strings.
list_ids = []
for i in range(0, 20000):
n = random.randint(0, 1000)
list_ids.append(
"Random_" + str(n) +
"/" + str(i))
# Connect to database.
client = MongoClient(
"mongodb://127.0.0.1:27017")
db = client.test_database
# Try without and then with an index.
for test_index in range(0, 2):
# Reset.
db.random_collection.delete_many({})
# Drop index.
try:
db.random_collection.drop_index([(
"code", 1)])
except:
print(
"INDEX NOT DROPPED")
# Create index on second test.
if test_index == 1:
print(
"CREATE INDEX")
db.random_collection.create_index([(
"code", 1)], unique=True)
# Insert documents from random data.
for random_string in list_ids:
db.random_collection.insert_one({
"code": random_string,
"type":
"random"})
# Find inexpensive houses.
print(
"FIND")
print(
"TIME 1:", time.time())
# Find values many times.
count = 0
for i in range(0, 20000, 20):
cursor = db.random_collection.find({
"code": list_ids[i]})
# Evaluate the cursor.
for c in cursor:
if c != None:
count += 1
print(
"TIME 2:", time.time())
FIND
TIME 1: 1509038951.0920522
TIME 2: 1509038959.902277
INDEX NOT DROPPED
CREATE INDEX
FIND
TIME 1: 1509038964.8188376
TIME 2: 1509038965.1191866
Time without index: 8.81 s
Time with index: 0.30 s