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