You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
4.1 KiB
Plaintext

00:02 Let's review the core concepts around
00:04 creating MongoEngine entities.
00:06 We started out by creating what I call basic classes.
00:09 These are classes that could just as easily have been mapped
00:12 to a relational database,
00:14 because you just have flat fields or columns,
00:16 if you want to think of them that way.
00:18 And none of the nested or particular capabilities
00:22 of document databases.
00:24 The one that matched that was the snake.
00:26 The snake, we make sure that it derives
00:28 from "mongoengine.Document,"
00:30 and then we specify the fields by
00:33 passing along or creating these MongoEngine descriptors.
00:36 So we said there's a registered date,
00:38 and that's a mongoengine.DateTimeField.
00:40 The length, that was a float.
00:41 That was the length of the snake in meters.
00:43 The name of the snake is a string,
00:45 species has a string as well,
00:47 and whether or not it's venomous,
00:50 is a Boolean, true or false.
00:52 So you can see we can map out the types
00:54 for this basic snake class, really easily here.
00:57 Of course our snake should have default values,
01:00 constraints like required fields, and things like that.
01:03 So here we've taken that same snake class,
01:06 and we've added a default value for the register date.
01:09 We said just call the function "datetime.datetime.now"
01:12 anytime you insert a new snake.
01:14 So it's going to automatically tag that new entity,
01:18 or that new document,
01:19 with the date in which it was inserted.
01:21 Now remember, be super careful.
01:23 Do not call the function now,
01:24 pass the function now.
01:26 Okay, we also set the length to a float.
01:29 We said that's a required float.
01:31 You have to specify the length
01:32 or MongoEngine will give you an error.
01:35 So you can't insert this thing; that field is required.
01:37 It's interesting that that's not
01:39 a feature of MongoDB, that's a feature of MongoEngine.
01:41 So by using MongoEngine instead of, say, PyMongo,
01:44 we get these additional features, same for the default.
01:47 And name, species, and venomous,
01:49 also these are all required, so we can do this here.
01:52 Now again, this is still one of these sort of basic
01:53 classes with just our constraints and defaults.
01:57 Let's look at the cage.
01:58 The cage takes better advantage
02:01 of the document database.
02:03 So we have the name, the price, the square meters,
02:07 required standard stuff there.
02:08 We also have the bookings.
02:10 These are either the times in which a cage can be booked,
02:13 or an active booking where a snake has registered
02:16 to be there at a certain time.
02:18 We model that through the booking class,
02:20 and we said this cage is going to embed
02:23 the bookings into it.
02:24 So to do that,
02:25 we use the MongoEngine Embedded Document List Field.
02:28 So a list of embedded documents,
02:31 and the argument would pass
02:32 as the type of embedded document.
02:33 So it's a booking that we'd put in with this list.
02:38 How does this look if we populate this cage
02:42 we add a couple bookings and we call save?
02:44 It looks like this.
02:46 It has the standard fields, right,
02:48 like an autogenerated_ID, the date that was registered,
02:52 this is set as a default value in the full class.
02:55 We have the name, the price, the square meters, and so on.
02:58 So that's all standard stuff, and we've seen that before.
03:01 But the bookings part, check that out.
03:03 So we have bookings, and it's a list, right?
03:06 Square brackets, not technically an array,
03:08 in JavaScript, right?
03:10 And the items in this list are those bookings.
03:14 We have a check-in date, check-out date in a range.
03:16 We have added two bookings in here.
03:19 Now we didn't fill out the, they're not booked,
03:22 we don't have a guest snake, and an owner ID,
03:25 and they haven't already taken them,
03:27 so they haven't rated it or given a review.
03:29 Some of the pieces are not saved into the database
03:31 to save space.
03:32 Nonetheless here we have our embedded bookings inside
03:36 of our document and we did that through
03:38 the Embedded Document List Field.