Eve: Building RESTful APIs with MongoDB and Flask Transcripts
Chapter: Why MongoDB?
Lecture: Why Mongo is a good match for REST

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In this lecture, we are not going too deep
0:02 into what Mongo is and how it all works.
0:05 You can find all sorts of information about it on the internet.
0:09 There is also a Mongo installation lecture
0:11 in the appendix should you need details
0:14 on how to properly set it up on your machines.
0:16 You might be wondering though
0:19 why Mongo was picked as the default db engine for the Eve framework,
0:22 when there were in fact so many other options around,
0:25 especially in the SQL ecosystem.
0:28 After all, when the Eve project was started,
0:31 Mongo certainly wasn't as popular as it is today.
0:35 In fact, the No SQL world have just surfaced on the public scene back then.
0:40 In hindsight, I believe it proved to be a great choice,
0:44 but allow me a few minutes to explain why I thought
0:47 right from the beginning that Mongo was going to be
0:50 a perfect match for a RESTful service.
0:53 Mongo stores data in flexible Json like documents,
0:57 meaning, fields can vary from document to document
1:01 and data structure can easily be changed over time.
1:05 But more importantly, as you know,
1:07 RESTful services usually communicate with the clients via Json.
1:11 So let's look at a typical get request,
1:15 we have a client somewhere and it is accepting the Json media type.
1:19 On the other side, we have Mongo,
1:21 which is storing native Json documents as well.
1:25 So maybe we can push directly to the clients from Mongo?
1:29 Ideally, yes, there is little working wall
1:34 as Python dictionaries naturally map to Json documents.
1:37 The same holds true when clients are writing to the service.
1:42 Here is a post request,
1:45 Json coming from the client that is received from the service
1:48 validated and then stored on the database.
1:52 And what about queries?
1:55 Well, it turns out that in Mongo
1:58 queries are also represented as Json style documents.
2:01 Here we are looking at a query performing in the Mongo shell,
2:04 now, if you were in SQL world,
2:07 this will be a typical select all documents from table things
2:11 where x=3 and y=4.
2:15 As you can see, the actual query is in fact a Json document itself.
2:22 A Json document is the perfect candidate for a URL query definition, isn't it?
2:27 We still need to perform some validation,
2:30 then, we can simply forward a query to the database.
2:33 The result will be of course a Json document again.
2:37 So as you can see, we're essentially going to have Json all along the pipeline,
2:43 or, if you will, Mongo and the REST service are speaking the same language
2:48 and in general, mapping to and from the database feels more natural.
2:52 There is no need to involve any kind of ORMs
2:54 which would end up making the service more complex
2:58 and more importantly for us, will probably have an impact on performance.
3:03 As we mentioned already, Mongo is schemaless.
3:06 Now, this doesn't really mean that we don't need a schema.
3:10 most likely, we still want to validate what is coming into our system
3:15 so we want to define some validation rules for known fields
3:19 we will see that Eve actually also supports
3:22 storing of unknown fields and documents.
3:25 But that is an opt in.
3:27 By default, when you are storing data on your service
3:29 you're guaranteed that unknown fields are rejected,
3:32 still, not having to worry about updating your db schema
3:37 as your service and requirements evolve
3:39 is very powerful and allows for less downtime
3:42 not to mention migration headache.
3:46 Lastly, let's talk about transactions.
3:48 One complaint that you usually hear about most No SQL databases
3:52 is that they are not transactional.
3:54 That can be severely limiting depending on your usecase.
3:59 But, if you think about the REST architecture
4:02 and how the first principle of RESTful design
4:05 is the total lack of state on the service,
4:07 you see how transactions don't really pretain to REST services.
4:11 You usually end up performing as more atomic updates anyway
4:15 because that is the nature of REST services.
4:18 So yes, Mongo doesn't have transactions,
4:21 but RESTful services don't usually have them either, so no big deal there.