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.

66 lines
4.1 KiB
Plaintext

00:01 Let's look at how we can do some basic crud operations
00:03 and connect to MongoDb with Python via PyMongo.
00:06 So if we're going to use PyMongo, let's start by importing PyMongo,
00:10 and I'm going to not import the items or the classes out of this
00:14 but actually just the module and use the name space style
00:17 to make it really clear where this stuff comes from.
00:19 Actually I like to do this in a lot of my programs, even in production.
00:23 So we import PyMongo, and then we have to create a connection string
00:26 and feed it off to the pymongo.MongoClient, right
00:30 so this is a concrete class in PyMongo,
00:33 and we can give it any sort of connection string,
00:36 in fact if you give it no connection string, I think it'll use
00:38 what I have written here basically, no auth, no ssl,
00:41 local host 27017 which is the default standalone MongoDB port.
00:45 Alright, so this is cool, we've got our client here,
00:47 and now then it gets a little bit trippy,
00:49 a little bit dynamic here, which is kind of fun.
00:51 So the next thing we're going to do,
00:53 is we are going to go to the client, we're going to say . some database name,
00:56 not table name, database name.
00:58 Now, this thing doesn't even have to exist at this point
01:01 this, as you saw on the demo, is actually how we created
01:04 this database called the_small_bookstore,
01:06 we just said db = client.the_small_bookstore
01:09 and by basically saying that it exists, or implying that it exists
01:12 it's going to since we do some kind of write, or modifying operation to it.
01:16 Ok, so just be aware that this is case sensitive, right,
01:19 so capital T capital S capital B, would not be
01:22 the same database as lower case t s b.
01:25 Right, so let's go, and now we're going to actually do
01:27 a lot of things that look extremely similar to what we saw in the Javascript shell,
01:31 that's why I spent so much time in that section
01:33 it's because the apis are so, so similar at this level.
01:36 So now we can just operate on the database via collection
01:40 so just like we said client.database name,
01:42 we're going to say db . collection name
01:44 and those collections also don't necessarily have to exist,
01:47 even for queries, if they don't exist, you just get nothing back that's not an error.
01:51 So for example, we can do a query against the books collection
01:55 and ask how many there are, so db.books.count
01:58 and that'll tell us how many books there are
02:00 and like I said, even if the database doesn't exist,
02:03 if the collection doesn't exist or both, it's still going to work,
02:05 it will just return zero, because guess what,
02:07 there are no books in the nonexistent database.
02:10 We could do a find_one and this will pull back just one item
02:14 by whatever the default sort the MongoDB happens to be using
02:17 and we can say find_one and give it
02:21 one of these prototypical not json but Python dictionary type of objects.
02:25 Now this find one is the first place where we're seeing the Python api
02:28 ever so slightly vary from the Javascript api;
02:31 in Javascript it's findOne, and in Python it's find_one
02:38 and they've adapted the api to be pythonic, right,
02:41 it would look weird to say findOne,
02:44 but just be aware that they're not identical, you kind of have to keep in mind
02:47 which language you're working in, but other than that,
02:49 what you feed to it and how they work it's more or less the same.
02:52 If we want to insert something we say db.books.insert_one
02:57 and then we give it the document to insert
02:59 and we get a result and we saw that the result actually comes back
03:03 and has an inserted _id and the inserted _id is the generated id of the thing
03:09 that was autogenerated in the database, notice we didn't pass _id,
03:14 but if we care we can get it back for whatever purpose.
03:17 When working at higher levels with like MongoEngine,
03:19 this will automatically just happen on the class
03:21 and get set we won't have to worry about it.