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