Skip to content

Commit f8d917f

Browse files
committedOct 9, 2017
Transcripts for chapters 4, 5, and 6.
1 parent 6c0f760 commit f8d917f

File tree

29 files changed

+1901
-0
lines changed

29 files changed

+1901
-0
lines changed
 

‎transcripts/ch4-mongo-shell/1.txt

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
00:01 So we've talked a lot about NoSQL document databases and MongoDB.
2+
00:05 Now it's time to actually start using MongoDB.
3+
00:08 So what we're going to learn in this chapter is twofold:
4+
00:11 one, how do you connect to it and manage it,
5+
00:13 with the management tools if you will,
6+
00:16 that is more or less the shell, and some additional tools,
7+
00:19 but also how do you query it from that shell.
8+
00:22 So maybe in Python in a traditional relational database
9+
00:25 you might be using say SQLAlchemy to talk to a relational databases,
10+
00:29 so you wouldn't necessarily use SQL, the language, in Python
11+
00:32 but if you want to connect to the database directly and work with it
12+
00:35 then you need to use ddl and SQL and things like that,
13+
00:38 there is the same parallel here in that we're going to use the shell
14+
00:41 and we need to use MongoDB's native query syntax
15+
00:44 which turns out to be very similar to Python's under certain circumstances,
16+
00:47 so it's going to be serving dual purpose there.
17+
00:50 So the primary MongoDB shell is a command line tool, right,
18+
00:55 we just type mongo name of the server, some connection string options,
19+
00:58 you can see all that the title here in this terminal.
20+
01:02 And then we just issue commands like if I want to go and use
21+
01:05 the training database out of the server, I'd say use training;
22+
01:09 and if I want to say go the courses and find
23+
01:12 the one with id 5 and display it not in a minimized, minified,
24+
01:16 but in a readable version, I would say db.courses.find
25+
01:20 and I'd give it the little json thing, id is 5 and I'd say pretty,
26+
01:25 So this is going to be entirely done in Javascript,
27+
01:28 so these statements that you type here,
28+
01:31 although you don't see any semicolons,
29+
01:33 these are either shell statements like use training
30+
01:36 otherwise, they're entirely pure Javascript.
31+
01:39 So what we're going to do is we're going to learn the Javascript api
32+
01:43 to talk to MongoDB, to query MongoDB,
33+
01:45 to do all the crud operations, there's a find, there's a delete,
34+
01:49 there's an insert, there's an update, of course there's sorts, there's upserts,
35+
01:52 there's all the things you would do in a standard database,
36+
01:55 the query syntax uses sort of a json model to help represent
37+
01:59 either operators or hierarchies and things like that.
38+
02:03 Now, you may be thinking, Michael, I came to a Python course,
39+
02:06 I don't want to learn the Javascript api, I want to learn the Python api—
40+
02:09 you will, you will learn the Python api for sure,
41+
02:12 and luckily, it's really, really similar, it's not identical,
42+
02:15 they made the Pythonic api Pythonic
43+
02:18 and the Javascript one follow the idioms of Javascript,
44+
02:20 but nonetheless, other than the slight like variations
45+
02:23 in naming around those ideas, they're basically identical,
46+
02:26 in Python we would use {_id : 5 } as a dictionary,
47+
02:31 here we use it as a json object;
48+
02:34 so on one hand, learning the Javascript api
49+
02:36 it is more less learning the Python api.
50+
02:38 But on the other, if you work with MongoDB,
51+
02:41 if this drives your application and you actually work with Mongo, in a real way,
52+
02:45 you will have to go into the shell, you will have to talk to the database directly,
53+
02:49 you have to maintain it, and manage it, and back it up, and do all those things;
54+
02:52 in order to do that, you need to know the Javascript capabilities,
55+
02:56 the way to do this in Javascript, as much as you do the Python way.
56+
03:00 Ultimately, the end game is to use something like MongoEngine
57+
03:03 which is equivalent to SQLAlchemy, sort of analogous to SQLAlchemy,
58+
03:08 in that we won't even be speaking in this syntax,
59+
03:11 but still, you'll need to know how these translate down into these queries
60+
03:15 because you might want to say add an index
61+
03:17 to make your MongoEngine perform much, much faster, things like this.
62+
03:21 So we're going to focus on Javascript now, and then for the rest of the class,
63+
03:26 we're going to basically be doing Python, but like I said,
64+
03:29 in order to actually use, manage, run,
65+
03:32 work with an application that lives on MongoDB,
66+
03:34 you have to be able to use the shell, and to use the shell you do Javascript.
67+
03:38 So just like anybody who writes web apps, we're all Javascript developers,
68+
03:41 if we write any form of web app, similarly here,
69+
03:44 if you work with MongoDB, we're all Javascript developers
70+
03:47 and we got to do just a tiny bit, but you'll find it like I said,
71+
03:49 it's super, super similar to what we're going to do in Python.

‎transcripts/ch4-mongo-shell/10.txt

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
00:01 So here's an interesting question— what if I want to find all the books
2+
00:03 where user 720 has rated that book exactly a nine.
3+
00:09 You would think that this would do it, right,
4+
00:11 we're using both values in this prototypical object or this document here
5+
00:15 and it says that the book is going to have to have
6+
00:18 a rating of nine and user id 720 has rated it.
7+
00:21 However, when we run this, you'll see we get mixed results.
8+
00:24 The bottom one looks perfect, we got a book with the user id 720
9+
00:29 an a value of nine in the ratings, great;
10+
00:32 but this other one, what's up with this, the red one?
11+
00:34 Well, user 601 rated this as a nine,
12+
00:38 and user 720 actually hated the book, they gave it a one.
13+
00:41 However, taken as a whole, does the book have a rating by user id 720— yes,
14+
00:46 does it have a rating of nine— yes, so it matches this and clause.
15+
00:49 So, oftentimes if you're looking for this exact subdocument match
16+
00:54 and that thing you're looking in is an array
17+
00:56 so ratings is an array of documents, if ratings was one subdocument,
18+
01:00 this would work fine, but if it's an array and you want to say
19+
01:04 I need to make sure that the thing in that array is
20+
01:07 that subdocument itself matches value and user id as I've specified here
21+
01:11 you need a different query operator, and that is dollar element match;
22+
01:15 so you can run this and it'll look down inside and say
23+
01:18 I want to find all the things in ratings,
24+
01:21 where both, the user id is 720 and the value is nine.
25+
01:25 So this is a slightly more complex version
26+
01:27 that you have to run and you have to use
27+
01:29 because you run into that problem we had before
28+
01:31 where somebody voted a 9, user 720 voted,
29+
01:33 but it was not user 720 who voted nine.
30+
01:35 So a little bit different than if you were working in
31+
01:38 say a sequel traditional tabular language
32+
01:41 because you don't ever have this kind of duplication within the one result,
33+
01:45 so it would be a lot simpler, but this is something
34+
01:48 that you kind of got to get your head around a little bit,
35+
01:50 you luckily don't use it very often, and if you are using the higher level of things
36+
01:54 like MongoEngine, you won't run into it,
37+
01:56 but down here at the shell or in PyMongo,
38+
01:58 you have to be really careful if this is actually
39+
02:00 the question you're trying to ask and answer.

‎transcripts/ch4-mongo-shell/11.txt

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
00:01 So, we're pretty good at finding and filtering down our result sets.
2+
00:04 The other super important things that databases do
3+
00:07 is to sort them, put them in order, so I would like the best selling book
4+
00:12 and then the second best, and then the third best in this category,
5+
00:15 that's a perfect sort by category, order by best selling this, right.
6+
00:20 So how do we do that in Mongo?
7+
00:22 Let's go over here and it turns out that there's a sort that we can run,
8+
00:25 and the sort takes something, right, kind of like our projection does here,
9+
00:30 so let me just show you before if I run this that this is not in order,
10+
00:33 so here we have c, c, d, f,  and then t, p, w,
11+
00:40 and eventually we're back just, you know, something before w,
12+
00:43 it is not sorted by title, not sorted by published date either,
13+
00:47 these three seem to be descending but the next one is not, ok.
14+
00:50 So it's not sorted at all, it's just however it comes back,
15+
00:53 probably by object id or something like this.
16+
00:56 Anyway, let's go and sort it, so let's suppose I would like sorted by title;
17+
01:00 so very much like our filter thing or maybe even closer, actually,
18+
01:05 like our projection here is I can come say I would like to sort
19+
01:09 and then this part that goes here, this one is ascending, right,
20+
01:13 so something that is positive means ascending, if it were negative,
21+
01:16 it would mean go in reverse order.
22+
01:18 So let's run this, now you can see, actually this is the beginning of the title,
23+
01:22 this exclamation mark and then some other exclamation marks,
24+
01:25 and then let's get past the symbols, a lot of symbols, anyway,
25+
01:30 you can see this is sorted by this, sorted by the title, not sorted by date,
26+
01:36 1994, 1993, 1996, we can also sort by date, let's comment this out,
27+
01:41 say .sort, published, let's sort in reverse order,
28+
01:47 newest which was 2050, I think we might have been fooling around with that
29+
01:52 or no actually I don't know where those came from.
30+
01:55 Anyway, 2050, 2038, 2037, 2030 and so on.
31+
01:58 Obviously, sorted in reverse order.
32+
02:01 What if I want to sort by the title and then any time the title matches
33+
02:05 I want to see the newest one of those.
34+
02:08 We can do that as well, so very very similarly we can say sort
35+
02:14 and then we just give it one of these objects with multiple values,
36+
02:16 so you want to sort by title, there's your sort by title ascending
37+
02:22 and then after that, if any of the titles match,
38+
02:26 let's show the newest one first, so sort by title ascending
39+
02:30 and then published descending, let's try that.
40+
02:33 Great, ok so here notice that these titles are the same,
41+
02:36 you might have noticed that before, but here's 1994 and here's 1993,
42+
02:40 so any time the title matches, we get the newest one first,
43+
02:44 I don't know if any others are in here with title matches.
44+
02:46 This first one must prove it right, this is how it works,
45+
02:50 sort by that and then by and you can have as many then buys as you like
46+
02:54 and they can either be ascending or descending,
47+
02:57 so here we're sorting by title first and then by published.
48+
02:59 The other thing that's important to notice is
49+
03:02 everything in MongoDB is case sensitive, when you're working with strings
50+
03:05 so that's probably going to play into this somewhere along the way.
51+
03:09 All right, so sorting pretty straightforward, just use these field names
52+
03:13 and then the direction you want to sort.
53+
03:15 The other thing that's worth paying attention to is
54+
03:18 you are going to want to make sure that you have an index
55+
03:20 so this sorting is actually fast, and we'll talk about that
56+
03:22 when we get to the performance section.

‎transcripts/ch4-mongo-shell/12.txt

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
00:01 Let's review sorting as a concept.
2+
00:03 So there's a sort function right on the result set
3+
00:05 on the cursor that comes back from find,
4+
00:07 and the way it works is we pass it some prototypical json document;
5+
00:11 but now instead of equality meaning matching,
6+
00:14 it means tell me the thing and the direction that you want to sort.
7+
00:17 So here we want to say sort all the books descending
8+
00:21 show me the most recently published to the oldest, right,
9+
00:25 show me the most recent books basically.
10+
00:27 Now this works pretty well, we could put anything that has a direction
11+
00:29 like a minus one, or one, I think you could even put higher multiples
12+
00:33 like ten and 20, 50, -10, but use one and minus one, keep your sanity.
13+
00:37 So this works well for one field, if we want to sort just by published,
14+
00:40 but if I want to sort by one thing, and then another,
15+
00:43 well we just put more into this document that we passed to sort,
16+
00:46 so we're going to say sort by title ascending
17+
00:49 and then sort by published descending,
18+
00:51 we run this, we saw that we get the results in our demo,
19+
00:54 first we sorted ascending by the title and any time they matched
20+
00:59 we sorted descending by the publish date.
21+
01:02 So first the 1994, A Nutshell Handbook, and then the 1993 one.

‎transcripts/ch4-mongo-shell/13.txt

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
00:01 Inserts are one of the simpler operations in MongoDB actually.
2+
00:04 So we just go db.collection name, in this case db.book.insert
3+
00:08 and we give it the thing to insert.
4+
00:10 Now, if we don't specify an id, an _id
5+
00:13 which generally we want to let the database generate
6+
00:16 but it's not always true, like we could have people and their primary key,
7+
00:20 their id could be there social security number, and that you would provide,
8+
00:23 so in this case, we're not going to provide an id,
9+
00:26 we're going to type in title and isbn, and those kinds of things.
10+
00:29 And then if we just do a find, that would come back and get the first one
11+
00:32 maybe say this is our first insert, we'd get something back like this,
12+
00:35 let's say we specified the isbn, the title,
13+
00:37 the author, the published and the publisher,
14+
00:40 this is a relationship over to the publisher table,
15+
00:42 which we haven't played with yet.
16+
00:44 So those were all set by us, you can see "Winning With MongoDB"
17+
00:47 and down here we have "Winning With MongoDB:,
18+
00:50 but the _id, because we didn't specify it was auto generated to an object id.
19+
00:54 So unless you have a good reason to pick another type of id,
20+
00:57 this is probably the best one for Mongo, but it could have been a string,
21+
01:00 like I said, it could have been a social security number
22+
01:03 or it could be just numerical if you want to have a 1234,
23+
01:06 all of those kind of put the burden on you to manage the uniqueness of that id
24+
01:10 and there is a unique disconstraint on _id for every table or collection.
25+
01:15 So that's how inserts work, you just give it this document
26+
01:17 and it stores it more or less directly in the database
27+
01:20 except for that it will generate this _id as an object id if needed.

‎transcripts/ch4-mongo-shell/14.txt

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
00:01 If inserts are simple, updates maybe not so much.
2+
00:03 In fact, there are two types of updates that we're going to look at;
3+
00:06 first, we're going to look at what is the conceptually more simple one,
4+
00:09 but also slightly more problematic.
5+
00:11 So I'm going to call this the whole document update
6+
00:14 and the way you might use this is you might go to the database,
7+
00:17 do a query, get a document back, make a change to it
8+
00:19 and say here, push this whole document
9+
00:22 back over top the existing one in the database, kind of orm style.
10+
00:26 The other one that we're not talking about here would be the in place updates,
11+
00:31 so you might say go increment the view count of this post
12+
00:34 without retrieving it, without changing the other parts,
13+
00:38 ok, so how does the whole document update work?
14+
00:40 Well, first of all, we're going to do an update
15+
00:43 if we come back and we look at it, we'll see maybe we've changed the title here,
16+
00:46 the author is still the same, but we had to pass the author,
17+
00:48 we had to pass the published and the isbn back,
18+
00:52 okay, in fact also the id, so all that stuff we had to put back,
19+
00:55 basically the way it works is we're going to do a where clause here
20+
00:58 so find it by the primary key, this great long object id
21+
01:02 and then here is the entire whole document
22+
01:05 we want to replace that document with.
23+
01:07 Now because of the way it's working here,
24+
01:09 there's a couple of features or settings you might want to control here,
25+
01:12 so you might need to set these, you might not depending on what you're doing,
26+
01:16 the default is if the where clause does not match, nothing will happen,
27+
01:20 there will be no kind of upsert, there will not be a new document added
28+
01:24 because we didn't find one, just nothing happens.
29+
01:26 So if you say upsert is true and you run this update,
30+
01:29 it will say I didn't find this document, so let me create it for you,
31+
01:32 so you could control that here.
32+
01:34 Similarly with multi equal true, normally unlike sql statements
33+
01:37 update only updates the first item it finds
34+
01:40 even if the where clause would match ten things, it only updates one of them.
35+
01:43 So that's a little bit funky, but if you think it's entirely replacing the record
36+
01:48 like why would that hole record be duplicated ten times,
37+
01:51 I don't know, it's kind of weird, but if you do want to update multiple objects,
38+
01:54 multiple documents in this collection, be sure to set multi to true,
39+
01:57 both of those orange values, their default values are false.

‎transcripts/ch4-mongo-shell/15.txt

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
00:00 After you've inserted some documents and maybe updated a few,
2+
00:03 it might be time to get rid of the old ones, so let's talk about deleting them .
3+
00:06 So again, it's db.collection name. and we're going to apply delete operation.
4+
00:11 And here we can say I'd like to delete one of them,
5+
00:13 delete one, or maybe I want to delete a whole set of them, right,
6+
00:18 the delete one we're passing in something that should be unique,
7+
00:20 like the primary key, and delete many, maybe a bunch of them have the title,
8+
00:23 maybe there is a couple of additions
9+
00:25 like a kindle and a paperback version or something like that.
10+
00:27 So just get rid of all of them with the title being some title.
11+
00:30 So, delete one, delete many— pretty straightforward.

0 commit comments

Comments
 (0)
Failed to load comments.