I was unsatisfied with out-of-the-box solution that mongoengine gave me for managing indexes. What they offer is adding some meta information to your model, and then it litters ensure_indexes
when making calls. There approach is shown here
I find that I need more finegrained control of defining indexes, and more importantly when to kick off there creation.
So I added an ensure_indexes()
method to my manage.py, and then dropped down to pymongo
to get the control that I want
It looks something like this …
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
I like using mongoengine’s get_db()
method to get a handle on a pymongo database object, it centralizes the connection logic which I keep in my database.py
file, which for completeness is here:
1 2 3 4 5 6 |
|
While figuring this out the following two docs to be useful: