Placeholder Image

字幕列表 影片播放

  • Hello and welcome to Mangane.

  • Be quick star with python manga.

  • TB is one of the most popular and exciting database technologies around Python is one of the most popular and fastest growing language there is.

  • And these two technologies work great together, and that's exactly what this course is about.

  • So we're going to quickly get started monger TB, write some code against it and build some realistic applications.

  • So the goal with this classes to teach him longer to be, we assume, you know a little bit of Python.

  • But you'll, of course, pick some things up along the way if you don't know it already.

  • And really, we want to get you going quick.

  • Hence the name Quick start.

  • So let's get started by talking about what we're going to cover in this course, we're gonna focus on three main things we're going to start with.

  • Why do you care about no sequel and document database?

  • Is how document databases make working with schema as easier modeling data easier as well as ad performance and flexibility to our applications.

  • We're going to talk about modeling, specifically causes one of the things that is pretty challenging about document database is if you come from a relational database world, you probably know about third normal form.

  • This is a way to carefully and structured way to plan out how you model your data in document database is you don't really have that.

  • There's a lot Maur sort of flexibility in how you design things.

  • This is great for you once you understand it and you're really good at it.

  • But it's challenging to know how to model things when you get started, because there's so much flexibility and it's kind of open ended feels a little more like art.

  • It's in science and in some sense, so we're gonna focus specifically on techniques and trade offs and some guidelines I've come up with to help you be successful modeling your data with document databases such as Mama D.

  • B.

  • And then we're gonna start writing code for really we're going to use a mongo odium object document mapper Think of like sequel alchemy.

  • But for document database is called Mongo engine, and we're gonna use that to create some classes to model or data and map those classes to and from Mongo, D B.

  • And use that as the foundation of our application.

  • Speaking of applications, what are we gonna build in this class?

  • Well, we're gonna build an Airbnb clone, but for snakes.

  • Okay, so we're gonna build snake BMB.

  • And this allows you when you're traveling with your snake, your pet snake, you don't want it tohave toe live out in the car or something like that.

  • You want to get us a cage, that it can live in a proper snake.

  • Kato will be happy, so they'll be owners of cages who can put their cages up for rent.

  • And snake owners, pet owners who want to travel with their pets and have their pet have a place to stay.

  • So we're gonna create this sort of silly Airbnb knockoff clone.

  • But we're gonna model many of the operations you would see in real Airbnb on our application here.

  • So it's gonna be pretty rich in terms of data as far as tools go.

  • Well, of course, we're gonna talk about Mongo D B, right?

  • That's the database we're using.

  • But you're also gonna learn some other things that are pretty awesome.

  • In the course, we're gonna talk about mongo engine.

  • This is the primary library we're gonna use to talk to Mongo D B.

  • It's built upon another one.

  • That's a very popular that is sort of the lowest level official way to talk to manga TV called Pi Mongo, so you might see a little bit of both of those.

  • Actually, in the course, we're going to use what I think is the best tool for accessing monger D B.

  • Something used to be called Robo Mongo, but now it's called Robo three T because I was acquired by a company called three D.

  • So this is part command line, part gooey way to interact with Mama D.

  • B.

  • And it's beautiful and free.

  • Open source.

  • It's great.

  • Also, we're gonna use pi charm for python code.

  • You don't have to use private part time for this course, but you'll see me using it, and I think you'll see a lot of benefits as we go speaking to me.

  • Who am I, anyway?

  • Who is this voice that you're listening?

  • Thio.

  • Hi, my name's Michael Kennedy.

  • You find me on Twitter at at Him, Kennedy, and what makes me qualified to teach this course?

  • Well, first of all, I host the most popular python podcast called Talk Python to me, and I've interviewed many, many people, including some of the folks from Mongo tbe the company as well as authors who've written about manga TV design patterns and things like that.

  • So I've had a lot of experience working with people in Python Ahmadi Be I've created the Talk Python training company and written many python courses and manga TV courses there as well.

  • And also, I'm part of the Mahdi Be Masters program.

  • So this is a group of about 35 external community members who give advice back to Mongo.

  • TB and I've been part of this for many years.

  • Work closely with the folks inside monger D B over the years.

  • So this is what you have in store for you.

  • Lots of awesome mommy to be in Python.

  • I hope you're excited.

  • Let's get right to it.

  • Let's begin by looking at why you might choose monger D B over other no sequel databases, other document databases or even other relational databases.

  • I'm sure you've heard of Mongo to be.

  • That's why you're taking this course.

  • But how popular is it relative to other databases?

  • Is it really the right choice.

  • Well, let's look at some data from 2017 if you compare monger to be against other no sequel databases.

  • Cassandra Couch to be raving d be things like that you will find one of these databases is unlike the others.

  • Look at that.

  • That's Red Herbal.

  • How much more popular manga TV is over these?

  • And it's not just the popularity that it's 5 10 times, maybe 50 times more popular than Raven Devi.

  • That's great, is very important, but also the trend right.

  • These others are either flatter, trending downwards.

  • And this is, besides little blip here based on probably like the end of the year numbers or something like that at the end of the data.

  • This is a really big deal, right?

  • This is incredible how much growth that is here, and it's still going up.

  • So manga TV is really, really popular in terms of a database that people are using, and that's great because that means it's well tested when we get to sectional bit later, we'll look at some of the users of manga D B.

  • But it's really important that there are some heavy workloads put onto these databases that you're gonna depend upon.

  • And if it can take what those people are doing, surely it can take what you have to throw it as well.

  • Now, among Devi is also loved.

  • If you look at Stack overflow at their 2017 developer survey, and you look at the most loved databases thes our databases that the developers are using currently and how they feel about it.

  • You see Mama TV ranks right near the top.

  • So definitely 55% of people who are using money to be Lovett compared to, say, like sequel Light or Cassandra.

  • So this pretty good boards even more interesting is that it's the most wanted database.

  • So these air technologies that you're not currently able to work with but you would like to.

  • So there are tons of people that want to work with monger D B.

  • But for whatever reason, they've got some legacy system built on my sequel or whatever.

  • They don't get the chance to.

  • So all these pieces of data tell you manga TV is a really good technology toe have in your tool belt, and it's easy to get a hold of write manga.

  • TB is open source and it's free.

  • You can just go to get up dot com slash manga TB slash mongo and clone it, and it's right there so you can see it has 11,000 stars, 3000 for X and the screen shot I took here.

  • It was updated two hours ago.

  • It's under very, very active development, so it's alive and vibrant.

  • Project Finally, If you actually want to get manga d b don't go to get up, Get up.

  • It's cool.

  • It's great they have the source, but don't go there.

  • What you really want to do is you want to go to manga tv dot com slash download dash center or just go to mommy, G b dot com and click download.

  • It'll take you here and you downloaded here.

  • You get it as binary.

  • So if you're on Mac, I recommend you home brew to install it.

  • But you can also download ah tar ball on windows, get the M s.

  • I.

  • And on Lennox, you can actually install it with aptitude or some package manager like that.

  • And of course, we're not gonna go to the details of setting up manga TV.

  • Just follow the instructions in the full Monty.

  • Be course you have.

  • We walk through all the steps, but we just want to get started.

  • This is the quick start, so let's keep moving.

  • Let's look at one of these records stored in manga TV to see how document database is work.

  • Here we have a Jason record.

  • This is actually from one of my courses.

  • The Python jump start by building 10 APS and this is how I represented in the database.

  • We've got standard column er type things, so we have an I.

  • D.

  • We have a title.

  • Of course I d.

  • On duration seconds.

  • These types of things Now, first of all, you might be wondering.

  • Wait a minute, Jason Database Jason databases that really efficient will probably not.

  • What Monger D B actually stores is a binary representation like a binary, token ized version of this record.

  • But they call it be Sahn because it's like binary Jason.

  • But we're humans.

  • We don't read by an area we re text.

  • So we're looking at the textural representation, so there's not exactly I'll get stored, but it's pretty close.

  • So we have these regular column type pieces of information I d entitle and so on.

  • We also have this other thing, these lectures.

  • Now these lectures air in this chapter represented by this record from this course and notice the bracket and JavaScript, which basically this is means that is a array.

  • And the array contains a bunch of sub objects.

  • So this is really interesting.

  • Instead of just having a chapter table and a lecture table on doing a joint or something like that, a foreign key relationship, we're actually putting the lectures inside the same record.

  • So this is pretty interesting.

  • When you look at it like this, you can imagine that this is like a pre computer join.

  • If I do a query for the lecture, say, given I d 1000 and one on, I get this record back, it already has the lectures.

  • I don't have to make another round trip to the database to get them.

  • I don't have to do a join against several tables in that original query.

  • It's literally a primary key query against an index primary key and is insanely fast.

  • And yet it already comes back with all this extra information.

  • So this is really cool.

  • If I get the chapter, I have the lecture is great, but you might be wondering, Well, really, what if I need to ask the question in reverse?

  • Like, fundamentally, if I need to get at lecture 10,106.

  • Will I be able to query manga TV quickly and efficiently to get that lecture?

  • And it turns out the answer is yes.

  • And that's why document databases are awesome.

  • It's not just like some nested blob stored in the original record you can as part of the query, language, and part of index is perverse, these hierarchies and very, very rich and powerful ways so we don't lose very much query capability just by putting the lectures in this one record.

  • So this is really neat, and this is sort of the foundational, most important takeaway from document databases.

  • We store them in these flexible Jason type of objects and weaken nest additional things like lists of numbers or even sub documents, as we have in this case.

  • Before we move on, let's take a moment and look at who uses monger D B now programming languages and databases and technologies aren't necessarily popularity contests.

  • Justcause simply is popular.

  • It doesn't necessarily mean that it's great, you know, evidence PHP, for example, or V B or something like this.

  • Technologies are popular, and some of them are great.

  • And sometimes great technologies are popular.

  • So there are some important things that come along with being popular and used in big, important companies.

  • And that means these things are durable and tested, and they've really been through the ringer.

  • So manga TB has been used by some really big customers and some really interesting use cases.

  • And I know some of the biggest ones are actually not on this page.

  • Even they're not listed here.

  • So we're taking a quick tour of a couple of customers who are using Market Eby, and we'll look at how they're using it.

  • Okay, so the 1st 1 that I want to look at, how screwed and having see some names that you might know some cool stuff here, Let's check out Royal Bank of Scotland.

  • So banks are supposed to be conservative right like this.

  • That probably wouldn't use weird no sequel document databases.

  • They're going to stick to their traditional oracle or sequel server.

  • Whatever.

  • Well, if we look at Royal Bank of Scotland, they're using mahogany be to support a global enterprise data service underpinning several core trading systems.

  • If your bank, the court trading system, pretty much of the center of the universe, right?

  • So the fact that they're driving that with Mongo and that's high performance and it's doing that for them, that's really awesome.

  • Let's check out Expedia.

  • So Expedia they have.

  • They have built on Mongo TV, and they're supporting millions of customers shopping for flights, hotels, cars, things like that.

  • That's pretty awesome.

  • Let's check out another one down here.

  • E a.

  • So video games, right?

  • This is the world's best selling sports game franchise, and they rely on Mongo D B to scale the millions of players at.

  • It's really awesome one more before we move on.

  • So these guys down here a little farther at sail through Still through is a company that does like outbound male and analytics and things like that.

  • So they are a marketing company very much doing tons of email type of stuff, like I said, and they have over 40 terabytes of data in 100 and 20 physical, mostly physical nudes, right?

  • So 120 servers, all working, running Margo D.

  • B in some giant cluster, which is a pretty awesome use case.

  • Of course, you have things like Shutterfly running among you to be squarespace on and on.

  • There's Aton of cool use cases down here, but I think you've got the idea.

  • Are you ready to model some really applications in manga d be using documents.

  • It's time to get our hands dirty and really start building something.

  • So in this chapter we're gonna go through our snake being be application, come up with the entities, the classes and tables or collections as they're called a manga TB and model them out.

  • So we're gonna first look at how this modeling and document databases compares to traditional third normal form modeling in relational databases when he's a pretty simple example.

  • But I think you'll get the idea and you'll see it much more in action when we build the real app.

  • So let's take this simple bookstore model.

  • Here.

  • We have books.

  • They have a bunch of properties.

  • We have people publish those books named publishers.

  • They have a name when they were founded, and you can navigate this foreign key relationship from publisher over to the publisher I D on the book now.

  • We also have a user, and a user might rate a book.

  • So we have users and we have radiance.

  • And we have a foreign key relationship between them and they know from rating over to book.

  • We have a one too many relationship there.

  • Write a book, can have many ratings.

  • We have a couple of foreign key relationships going on this place.

  • Now, let me tell you, in a really application, there'll be many more little tables with extra information like this, like ratings about books and so on and say reviews, for example, things like that.

  • Maybe even related items that we pre computed from some sort of machine learning.

  • But we want to store that in the database.

  • So imagine this model here having like 15 tables with relationships across all the various pieces back and forth, right, So I just want to keep it simple fits on screen.

  • You're not gonna go crazy with it.

  • So how would we model this in Manga Devi?

  • How will we model this using documents?

  • Well, you would see that it's somewhat simpler than Maur craziness that we had a relation of model, the sort of more contrast you will see here.

  • So we still have our publisher and they have their i D.

  • And when they're found it, we have a user and the same columns or pieces of information there as well seems book.

  • But now our ratings, we've decided, you know, when we get a book, most the time, we actually want to know the rating.

  • We want to say like this is a 3.4 star book or has 72 ratings even in a list.

  • We want to show that stuff.

  • So we're pretty sure we wanna have these ratings always with the books.

  • So why put him in a separate table?

  • Let's embed them now.

  • We still have some relationships like we had in the Relational model.

  • For example, we have our publisher idea on books and that links over to publisher.

  • Now.

  • This is what in Margaret's B I refer to is a soft foreign key constraint.

  • There's a relationship between books and publisher.

  • It's the publisher I d set to the idea of the publisher, but the database itself doesn't enforce this right, So you need to be a little more careful in your app and we'll see how we do that when we get to the coat.

  • But as I was saying about ratings, these were not gonna put in a separate collection.

  • In fact, we're gonna store those inside of our books so we can embed these objects and a raise of either straight values like numbers or strings or actual sub documents.

  • Like our ratings here to mine every 123 and so on is actually part of the record of the book.

  • So we're gonna get the book record back already have the ratings, things like that.

  • So again, we can think of this radiance Being embedded within books as a pre computer joint is a slight bit of overhead if you actually one of the book without the ratings and you're just gonna get him back and ignore them most of the time anyway, if that was the case, But if most of the time you do want the radiance, this is a huge speed up.

  • And like I said, imagine there were 15 tables before and five of them could be collapsed into the books.

  • They have a five way join going down to a single primary key query would be amazing as we discussed, modeling with documents and document database is is a little bit more art.

  • There's a little bit more flexibility and kind of just gut feel of how you should do it.

  • But let me give you some guidelines that will give you a clear set of considerations.

  • As you work on your data models, you'll see that the primary question for working with document databases is to embed or not to in bed.

  • That is when there's a relationship between two things in your application.

  • Should one of those be a sub document should be contained within the same record as the thing it is related to, or should they be two separate collections?

  • What modernity?

  • Because tables collections?

  • Because they're not tabular right?

  • Should those be two separate collections that just relate to each other?

  • So we're gonna try to answer this question.

  • I'll try to provide you some guidelines for answering this question, so the 1st 1 is is the embedded data wanted 80% of the time when you have the outer or other related object?

  • Okay, so let's go back to our example.

  • We just worked with we had a book in the book had radiance.

  • So to make this concrete, the question here is Do we care about having information about the ratings most of the time when we're working with books?

  • So if our website like list books and that that listing has the number of ratings and the average rating and things like that listed as part of the listing, you pull up a book, maybe the ratings and the reviews air shown right there.

  • Most of the time we have the book, we have ratings evolve.

  • Somehow then we would want to embed the ratings within the book, right?

  • That's what we did in our data model.

  • We said yes.

  • Actually, we do want the ratings most of the time.

  • Now, let's look at this in reverse.

  • How often do you want the embedded data without the containing document?

  • So in the same example, how often is it the case that you would like the ratings without the book?

  • So what use cases for maybe, like, where would that show up in her AP said, Like maybe you as a user, go to your profile page on the bookstore, and there you can see all the books you've rated right and the details about the rain you don't actually care about necessarily the books you just want.

  • These are the ratings that I've given to things, and here's my comments and so on.

  • I don't actually want most of the details, or maybe any of the details about the book itself.

  • So if you're in that sort of situation a lot of the time, you might want to put that knew a separate collection and not embedded.

  • You can still do it.

  • You can still do this query, and it will come back very quickly.

  • There's ways to work with it, but you'll see that you have to do a queer against the database and then a little bit of filtering on the application side, so it's not prohibitive.

  • It's not that you can't get the containing document with the contained document out.

  • It's a container, but it's a little bit more clumsy.

  • All right, So if this is something you frequently want to dio, then maybe consider not embedded in it now is the embedded document abounded?

  • Set.

  • So let's look at ratings.

  • How many ratings might a book have 10 ratings ah, 100 ratings.

  • 1000 ratings.

  • Is that number gonna just grow?

  • Grow the number of ratings that we have?

  • If this was like page views and details about the browser or I p address and date and time of a page reviewed, I would not make a a good candidate for embedding that, like safer views of a book, because that could just grow and grow and grow.

  • Is the popular in your sight grows?

  • And it could make the documents so large that when you retrieve it from the database, actually, the network traffic and the distract IQ would be a problem.

  • I don't really see that happening with ratings.

  • I mean, even on Amazon, like super popular books have hundreds, not millions of ratings.

  • So is probably okay.

  • But if it's unfounded set, you do not want to embed it.

  • And is that bound small right?

  • Like then maybe millions of views still of being recorded within sign of a book would be a really bad idea, And the reason is these documents are limited to 16 megabytes, so no single record a mongrel you be can be larger than 16 megabytes, and this is not a limitation of manga TV.

  • This is them trying to protect you from yourself.

  • You do not want to go on.

  • Just say query by, say I ESPN and try to pull back a book and actually read ah, 100 megabytes off a desk and over the network that would destroy the performance your database.

  • So having these very, very large records is the problem.

  • So they actually set an upper bound on how large that can be.

  • And that limit is right now, currently at the time of recording 16 megabytes.

  • But you shouldn't think of 16 megabytes as like, Well, if it's 10 megabytes, everything's fine.

  • We're still got a long ways to go now.

  • You should try to keep these, you know, in the kilobytes tens, twenties, hundreds of kilobytes, not megabytes, because that's going to really hurt your database performance unless some situation just makes the time of sense of these very large documents.

  • So having small bounded sets means that your documents won't grow into huge, huge, monolithic things that are hard to work with.

  • Also, how varied are your queries?

  • So one of the things that you do with document databases is you try to structure the documents to answer the most common questions in the most well structured way.

  • All right, so if you're always going to say I would like to on my pages show a book and it's related ratings, you would absolutely put the ratings inside the book because that means you just do a query against a book and you already have that, like, kind of pre joined data.

  • But if it's sort of a data warehouse and you're asking all kinds of questions from all different sorts of applications, then try and understand what what is the right way to build my documents so it matches.

  • The queries I typically do are the ones that I need to be really quick and fast.

  • That becomes hard because there's all these different queries and they have like one.

  • The way you model for one is actually the opposite of the way, the model for the other, right?

  • So depending how focused your application is or how many applications air using the database, you have a different answer This question to the more specific your queries are, the more likely you are to embed things and structure them exactly to match those queries related to that is, are you working with an integration database or an application database?

  • We'll get to that next.

  • So wait, what is an immigration database?

  • So if you were just working on your own personal website or some small project, you don't have an immigration database.

  • But if you work at a big enterprise, a big corporation where there's many internal systems, you may be working with the immigration database.

  • And, honestly, that's not a great fit for no sequel databases in general.

  • But it also makes designing documents for them more difficult.

  • So in large corporations we have many applications that share the same data.

  • One way that we have built applications to share data is to just share the same database, so we might have a bunch of different applications, and they're all going to talk to the same database.

  • So they all have the same concept of a user.

  • They all have the same concept of an order.

  • Things like that, and this pains the concept of the user is as complicated as it can get.

  • Maybe the application, the top left could have a real simple user of the one in the top right actually need something else.

  • The bottom one is something else.

  • Still, until you've kind of got a model across all these applications, that makes it super tricky.

  • Also, in new sequel databases and document databases, the relationships are enforced in the application.

  • So that means that all of these have to agree on what the constraints are, what the relationships are and that can actually cause did integrity issues.

  • So there's a lot of reasons that integration database isn't a great idea for relational databases.

  • In fact, it's not a great idea at all, but it has been used.

  • And because there's different applications with different query patterns, it makes designing your documents more difficult.

  • So instead, what do we do?

  • We build application databases.

  • Maybe we have a bunch of different applications, just like before.

  • But they all have their own data store, and they all talk to their own databases.

  • Of course, they need to exchange data like before, so maybe we do some sort of micro service thing where they talk to a service boss or they just talk to each other, things like that.

  • And that means each individual green database and interaction with its own application is super super focused, Unlimited.

  • And here in these cases, Margaret Evey document databases make a lot more sense, and it's easier to design the documents because the range of queries it's extremely focused so you can target those particular questions against a small set of queries guidelines we just talked about.

  • So this is the kind of model you wanna have.

  • If you're doing data exchange within your organization and you're working with a document database, I don't know about you, but I feel like we've talked about coding and talked about Mama TB in theory enough.

  • And it's time to write some code and use monitor, So that brings us to getting started with our demo application.

  • So throughout the rest of the course, we're gonna spend a significant amount of time focusing on this.

  • Remember, we're building Snake B and B.

  • This wonderful experience were snake owners and their pets can share other snake cages when they're traveling, so they feel totally comfortable on every vacation.

  • Mean to take your snake, of course, is just a knockoff of Airbnb type thing, and in this video, we're going to see how to get it from get up and how to get it up and running in python and pie charts.

  • So we'll start out over here on get up dot com slash mike seeking any slash monkey to be that quick start dash course.

  • And you can see that we've got a couple of things here.

  • We've got some data.

  • This is empty right now, but I'm gonna fill it up with stuff as we go through the classical bill to recreate the database will be instructions in there and how to restore that.

  • And then if you go over the sources of the most interesting part, we're gonna be working in this area here.

  • But I've made a snapshot of starter code Snake BB.

  • This is exactly snapshot of what we're starting from.

  • Okay?

  • But I'm gonna be working in here because I want to have it built up, right?

  • Also, try to dio make some branches or other save points really obvious when we get to the various videos.

  • Right now, there's no other branches, but we'll get to those.

  • Okay, let's go and check this out.

  • We'll go copy what we need, and we'll say, Get clone this nice and quick and Let's get to work with it.

  • Over here, we have our source code and we have our snake BMB and we have our starter code Snake being be So These were the two projects here, and what I want to do is I'm gonna put this in the pie charm on Mac OS.

  • You can just drag and drop this on a pie chart and load the project from that folder.

  • However, if you do this on Windows or on Lenox, I think you have to go to Pine Charm.

  • It's a file open directory.

  • However, before I d'oh, let's go into this folder really quick and create a virtual environment.

  • So you may be familiar pylon and virtual environments and so on.

  • But if you're not, let me give you the quick overview of what's what's going on here.

  • If we look here, we're gonna have apparently a misspelled requirements file, which we're gonna take care of in a second.

  • But notice in this requirements, file these air the external libraries pie mongo on mongo engine for manga, TB and some other random stuff for working with color output on the console as well as parsing daytime's entered from the user.

  • So we need these libraries and we don't wanna install them and manage them.

  • Basically, as a machine wide thing, we would install them into our virtual environment.

  • So let's go over here and first name requirements, and we're going to go and actually create the virtual environment.

  • And then we'll install we install stuff into it.

  • So here we are again and the source folder So we'll say Python three dash M V Envy.

  • So run the virtual environment module into dot Envy this naming convention dot envy something that Pint Arm understands will automatically detect and start using in a past stashed copies flag that's only required on Mac OS, I believe.

  • But anyway, we'll go with that.

  • And if we do in L S H, you can see this hidden dot gov, but we don't need to do anything else with it.

  • Pie charm should take it from here so we can go and grab this folder on Mac OS memory file, open directory on the other oases and drop it here.

  • So let's go and tell pie charm about get the very first time travel index, the python environment.

  • We gave it, then it should be up and running.

  • Okay, so let's look down here in the terminal.

  • You should see the Dottie envy.

  • You can ask questions like which Python?

  • And it showed you.

  • It's the one that we created in Windows.

  • It's waiter Python.

  • Not which python have We go here, we have our requirements and so on.

  • Now the other thing we need to d'oh as we need to right click and say set this as like, the relative path.

  • So in this file, when I import some other file, it looks relative to that.

  • So I can say, right click here and say Mark directory as sources route or just be in this folder when you run it in python and basically you're working director.

  • Okay, so we're almost ready to run things.

  • The last thing we need to do is install these requirements so we can say pip install Dash are the requirements file and that will install those libraries for us.

  • When we run, the application has everything it needs.

  • So we try to run it now It'll crash and say it can't find Kalorama or something like that.

  • Now this application is empty.

  • It doesn't do anything other than ask for a couple of prompt.

  • There's no data access for manga to be anything in here, but let's go and just get it to runs.

  • We can right click on program and right click and say, Run program!

  • It runs and you can see I make it bigger Here we have our snake B and B, and I put a little snake there for you guys and ask you a question or your guests or hosts.

  • Are you looking for a cage or do you wanna offer up your cage?

  • So let's scare with guest, and it lets you do things like creating account.

  • Had your snake so on.

  • So I could say I like the law again so that, you know that's not implemented yet.

  • In fact, that's what we're gonna be doing.

  • Next is implementing all of these features in the database creating account, Log in in booking a cage, viewing cages, things like that, right?

  • So all the actions you might do in a typical Airbnb situation.

  • So that's it for now we have this up and running.

  • Let's do one more thing because of the output.

  • This I find this looks a little better if we just run it separate outside of pie charm.

  • So we can say copy of the path here.

  • And, um, we're still in this folder with the DOT environments.

  • We need to activate it if we're gonna run it over here.

  • So we would say dot space Thought he envies been slash Activate on Windows.

  • You don't need the first thought and it's not been its script, script or scripts.

  • I can remember.

  • I think it's scripts either way, a promise to change.

  • And now we can run this.

  • Here's your snake again.

  • Okay, so we're all set up and ready to run our code.

  • It's time to write some code against monger D B and Connect to Mama D.

  • B.

  • And we're going to do that with an ODM.

  • An object document mapper his terms knew.

  • Do you think of object relational mapper like sequel Alchemy, but for document databases instead?

  • So let's compare first, this podium style of programming against the most basic Louis level, way to program are interact with manga TB from python called Pi Mongo.

  • So every programming language that you can talk to Mongo TV from there's many of them 2030 or something like that.

  • Many, many languages can talk to Mom.

  • It'd be and they each have what's called a driver.

  • And this is typically provided by monger d B the company itself.

  • So pie mongo is no different.

  • It's this low level, foundational way to talk to mongo D.

  • B.

  • And you do this in the need of query syntax off monger D B this JavaScript Jason style of interacting with the database.

  • Now, it's important to know that if you're working with mongo d.

  • B in terms of running and managing, but from writing code, we're gonna focused on something higher level and odium so we can take structured classes and mapped those to and from the database.

  • So let's see how it would work if we just used my manga.

  • So we've got our app here, and we have the pie mongo package we're gonna work with and we have manga tbe the database.

  • So we write direct queries in this raw monger, D B a p I.

  • So you have to know the FBI really carefully.

  • You have to map those back to your class is basically what you do is you passed dictionaries too.

  • Hi, Mongo would use those as part of a query, and then you get the dictionary's back pretty unstructured, but it's very low level and fast within odium.

  • Similarly, we've got her app, and we've got pie mongo and manga TB.

  • But we also have another layer, the layer that we directly interact with, called the podium Object Document mapper.

  • And there's a bunch different kinds.

  • There's Mongo engine, There's Ming.

  • There's Mongo Kit.

  • There's Mongo, Alchemy, Mini Mongo.

  • And there's a more than that.

  • Actually, there's a ton of them.

  • Just so happens we're gonna use Mongol Engine, one of the more popular and well polished ones.

  • I think so.

  • In this model, we don't query in terms of raw dictionaries against pie mongo.

  • We just talked to the classes defined by the odium and our queries air.

  • Based on those types on those classes, that itself will translate to the manga D B.

  • A.

  • P.

  • I, sometimes in real basic ways, sometimes really advanced ways, and it will actually leverage some of the advanced operators.

  • The dollar operators.

  • If you're familiar with him from longer to be like dollar set dollar ad to set things like this so really really cool that it leverages the advanced operators not just, you know, save this document.

  • Read this document type programming.

  • I think the podium model is a much better way to write your application.

  • You'll see there's not much structure in a ski Melis database, so having a little bit of extra structure defined by these classes that are part of this sodium model really adds a lot of safety to your application.

  • In my opinion, the podium we're gonna use for this course is mongo engine, and you can find its home page and details about a documentation, and so on.

  • That Mongol engine dot org's so macho engine is open source.

  • Like many things you'll find in Python as we said it, a pins upon Pi Mongo.

  • You saw us install it earlier.

  • We just did Pip install Mongo engine and that installed pie Mongo along with it.

  • We did that through the requirements file, but you could do that directly.

  • If you prefer you confined mongo engine on get up.

  • You can see that it's quite popular.

  • Almost 2000 stars.

  • This is much more popular than the other Mongo D B podiums.

  • As far as I can tell looking the other ones.

  • This is definitely the most popular, at least among the most popular of them, and it's very actively under development.

  • I just took the screen shot right now before start recording here to give you the latest version, you can see that it's been updated in the last 24 hours.

  • Smother stuff under the actual code has been updated in the last 21 hours.

  • So very active.

  • This is important for an open source project you're gonna depend upon.

  • So I think, you know, judging by that mongo engine is the best choice in the FBI's.

  • Excellent.

  • We're gonna start working with it next.

  • The first thing we need to do to start working with manga legend is tell Mongo Engine how to speak to Mongo D B.

  • Let's go back to our snake being the app we've been working on.

  • And we're gonna go in here to this data section aren't cutting to create a new file python file called mongo set up.

  • So in here, we're going to write a simple method that we can call from other places in, as you'll see in the slides that we look at later for riel applications that used proper connections, encryption accounts, things like that.

  • This could be a little more complicated, but it's going to start out pretty simple.

  • So we'll say global in it.

  • And in here we just want to call one function.

  • So we're gonna have tohave mongo engine here.

  • So in import Mongol engine and we just say mongo engine dot register connection.

  • And the first thing that we pass is an alias, so I'll be real explicit.

  • Alias equals core.

  • Now what is this alias thing about?

  • We can have multiple connections to even multiple databases or even multiple database servers registered here so we could have, like, a core data and analytics for just analytics.

  • That goes to a separate database that maybe has tons more data because it's like page views and actions and so on.

  • But we might keep that separate so we can back it up on a separate schedule, something like that.

  • And then we need to set the name of the database, and we'll set that to Snake B and B do a quick format here, and it's all good to go.

  • Like I said, this gets more interesting in real connections, but we're going need to call this to talk to Mom.

  • It'd be so let's go over tear our little program.

  • And you saw up here the top there was this to do set up mongo engine global values, and that was basically what we're doing there.

  • So we need to go over here and say we need to go data dot mongo set up as what's called mongo sent up, and they should be pretty simple manga set up dot global in it.

  • You know, we just need to make sure we call this once in our application, and we need to do this before we actually interact with anything else.

  • So let's go and apply these settings over to our energies as well.

  • So we look at snakes first.

  • Now, this model is going to be mapped into one or more of those databases.

  • Well, one among many potential databases.

  • So the way that we can tell it howto work mongo engine will use property.

  • We can add to it.

  • We can say meta equals and we give it a dictionary, and we can say d b underscore alias, and we'll say, core here.

  • Okay, tomorrow at it.

  • Let's say collection is snakes.

  • So even though we called a capital s snake the thing in the database where these records these documents are stored, what we called snakes plural, lower case.

  • And here we can tell if this goes into the core database online.

  • Maybe the analytics one or something like right.

  • We're only gonna have one in here.

  • But for our example, you want to have this year, so case you want to add more later, right?

  • We're going to add the rest of these two owners and cages, but not bookings, because bookings is going to be nested inside here.

  • All right, so we don't need to tell that how it gets stored cause it's stored alongside cage cages and no surprise owners to those are three top level entities that map two or three top level collections in manga TV.

  • Now we've registered the connection by using our mongo set of global, Which should we just call Richard a connection?

  • Like I said, this couldn't get way more complicated in reality.

  • And then we just go and we said this meta to use the core connection as well as naming the actual table collection it's gonna go to Let's take a moment.

  • Look, this concept of registering connections, we're going to need to work with language.

  • And so, of course, import Mongol engine, and we need to set the alias and the name of the database.

  • So it's a Mongol age and not registered connection.

  • Give the alias, give it the name we need to call this before we start interacting with our classes and other types before we try to do any queries or save any data, this has to be all set up.

  • So this is what we wrote in our application, and this works fine when you're talking to the local mongo to be running no encryption, no accounts default port running on the local machine.

  • Same machine is this code?

  • How?

  • If you're doing this in production, there's more to it.

  • All right, you need to set the user name and password what you have to set up in Manga Devi.

  • There's none by default, so you got to set that up with the right permissions.

  • Probably.

  • It's a different server on alternative port.

  • So set the host in the port.

  • You would like to create a sort of admin account, which is associated with that username and password.

  • So you say, Look, you authenticating admin used this mechanism.

  • Finally, if you're gonna d'oh connections across You know, somewhere outside your data center, you pretty much to just turn this on is you need to turn on SSL and configure this river for SSL.

  • Then you pass that additional data in addition to the alias and the D B.

  • Now there's a lot going on here and deployment and running Manga, TB and production is not a simple maybe as it could be.

  • Certainly it's something you need to be very careful about.

  • Like no authentication, no encryption.

  • All right, don't run your code that it's fine to do it for development.

  • But don't do it for production in my full mom.

  • It'd be course, I actually go into spend an hour ago in Crete Olynyk server and set it up in a cluster of the database and the web servers and those kinds of things and really make this work perfectly in safely.

  • But in this course, we're not gonna go into it.

  • I just want to leave you with you need to set us up.

  • You can look at the monitor v dot org's site as well among you to be dot com site and go through the documentation on some of the steps or just take my other course if you're really gonna go and use this in production.

  • So far, we've modeled in our data with python classes, but there was no real mongo engine entity stuff.

  • They wouldn't actually be saved or processed by mongo.

  • Engine just happens to be We could have sketched them out in Pathon class style.

  • So we're going to change that now.

  • We're gonna go make our standard playing old python classes proper mongo engine entities.

  • Snake is pretty simple.

  • So let's start there.

  • So in orderto work with Mongo Ridge and over here, I'm gonna have to import it now.

  • You might want Thio do from mongo engine imports from things, but I'd like to be real explicit, like these things air coming from mongo engine even in my production code.

  • So this is how I'm going to do it.

  • And we're gonna set the registered date to a particular type of descriptor that comes from mongo engine and add sort of the type level.

  • This tells Mongo engine.

  • What type of data and constraints and requirements go onto this field.

  • However, at runtime, it's going to act like, say, a daytime or whatever it is.

  • In this case, the daytime and species would be string, and so on will come over.

  • It's a mongo engine.

  • Don't date time Field and will to like this.

  • So this will tell Mongo engine to map that to the database.

  • As a date, Time field over here will say Mongo engine, not string field.

  • And over here, the length.

  • Let's say this is in meters, and that's probably decent.

  • Have flutes, right?

  • So this will be a float.

  • Field the name again.

  • A Stringfield.

  • Whether it's venomous or not, that's true.

  • False gets could be a number like level of venomous, I don't know, but we're gonna call this a Boolean field, and that's that.

  • So our snake is already to map in tomography.

  • Be good to be doesn't have things like required fields or default values or anything like that.

  • But Mongo engine does, so let's change this to make it a little simpler to create a snake.

  • So, for example, register date is almost always just whenever you inserted it.

  • Right.

  • So what we can do is we come over here and set a default function that will execute anytime.

  • Mongo, ancient inserts a new snake.

  • So we're gonna start with daytime.

  • And so the function that we want to call is the now function, which gives us a full year, month, day, hour, minute, second representation of time.

  • So we'll commit.

  • Has a date time, don date time dot Now and be very careful not to put the parentheses.

  • You're passing the function, not the value of now.

  • That would be like when the program started, so that could be a little tricky over here for species.

  • Let's say that you have to say the species so we can say this is gonna be required is true.

  • In fact, the length is required.

  • That name is required.

  • Whether it's venomous is required.

  • We

Hello and welcome to Mangane.

字幕與單字

單字即點即查 點擊單字可以查詢單字解釋

B1 中級

MongoDB與Python速成班--初學者教程。 (MongoDB with Python Crash Course - Tutorial for Beginners)

  • 2 0
    林宜悉 發佈於 2021 年 01 月 14 日
影片單字