MongoDB for Developers with Python Transcripts
Chapter: High-performance MongoDB
Lecture: Concept: Indexes via mongoengine

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Now if we're using MongoEngine,
0:03 we don't have to go to the shell and manually type all the indexes
0:05 we basically go to each individual top level document
0:08 so all the things that derive from mongoengine.document
0:11 not the embedded documents, and we go to the meta section
0:14 and we add an indexes, basically array
0:17 so here we want to have, you can see the blue stuff that's highlighted
0:20 we want an index on make, we want an index on service history
0:23 and within service history, remember these are service records showing on the bottom
0:27 we went an index the description and price.
0:30 So for index that we put 'make', that's straightforward
0:34 and then we have service_history.customer_rating
0:37 so service history is the field name
0:39 and then customer rating is the field name of service record
0:42 and for some reason I don't have it blue, it's that last one down there
0:45 but we also want this composite key
0:47 so service_history.price and service_history.description
0:50 we want to be able to find where both of those match
0:53 and we're going to do that up by having
0:56 a more complicated entry in the indexes bit here
0:58 this is going to be a dictionary where the fields are set
1:00 to be this array of strings and not just the flat string itself.
1:04 So once we add this, when we run our code,
1:07 it's actually going to first time we work with that document
1:10 ensure that all the indexes are there,
1:12 and remember that like hung up our application for just a little bit,
1:16 but the real benefit here is our app is always going to be in sync,
1:21 we don't have to go oh oops, I forgot to add the index,
1:24 that one particular index to say the staging server,
1:27 or when I push to production are there new indexes,
1:30 I got to go out on the database,
1:32 now you don't worry about that, you just push your code,
1:34 restart your web app or whatever kind of app it is,
1:36 and then as part of interacting with it,
1:38 it will make sure that those indexes are there.
1:41 If you don't want that pause to be there,
1:43 just go and create the indexes you know the thing is going to create
1:48 put them on the production server and then push the new version of code
1:50 and it will just go great, these indexes exist.