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