Some Python scripts related to MongoDB
mongoDB-Experiments.ipynb:
This script is made to demonstrate how you can use Python to understand and compare Query performance using "Explain executionStats".
The script has 2 Parts:
- Data setup: This part has functions that you can use to create dataset for the queries in the script
- Query Analysis: This part has 2 queries that I have used to display how their performance can be identified and compared.
-
First Query is a simple "find":
db.collection.find({"age":"20"})
If you have a mongoDB CLI or you can use any client like Robo 3T. You can get the Explain plan with executionStats using the following:
db.collection.find({"age":"20"}).explain('executionStats')
Test Cases:
Try1: Without any index
Try2: With index on "age" -
Second Query is an aggregate query:
db.collection.aggregate([{"$match":{"$and":[{"age":{"$lte":20}},{"gender":{"$eq":"Male"}}]}}])
This is a simple single stage aggregate with a 2 predicates to look at a compound index. If you want to check out its Explain plan with executionState:
db.collection.explain('executionStats').aggregate([{"$match":{"$and":[{"age":{"$lte":20}},{"gender":{"$eq":"Male"}}]}}])
Test Cases:
Try1: Without any index
Try2: With index on age
Try3: With compound index on age,gender
If you dont have a mongoDB instance, you can easily set one up using docker. Here is a simple command you can use to start a mongoDB container:
I have used mongo:4.4.12 for this experiment since I setup mongoDB on my RaspberryPi and this version is the last supported version on the platform. You can use the latest version of mongoDB as well.
Just remember to create a datadir directory in the directory where you run this command. That will store all the datafiles.
docker run -d --name some-mongo \
-p 27017:27017 \
-v `pwd`/datadir:/data/db \
-e MONGO_INITDB_ROOT_USERNAME=mongoadmin \
-e MONGO_INITDB_ROOT_PASSWORD=secret \
mongo:4.4.12
This will create a default admin user mongodbadmin with password secret
Your connection string, if you run this on your local system will be : mongodb://mongoadmin:secret@localhost:27017