Using PyMongo
Objectives
Use PyMongo to connect Flask server to a MongoDB database
Create blogposts
Review PyMongo API documentation
Build your own simple blog with Flask and PyMongo
http://flask.pocoo.org/ http://api.mongodb.com/python/current/tutorial.html
Connect to MongoDB
Flask recommends using a Python library called PyMongo to connect to and interact with MongoDB. You'll find that PyMongo is "pythonic" in that it's easy to use and it's designed to be readable. The syntax it uses almost seems like you're writing inside the Mongo shell itself.
First, use pip3
in your terminal to install PyMongo.
Remember to start your local MongoDB server:
mongo-server.py:
Refer to MongoDB documentation to find out more details about how to interact with MongoDB through PyMongo.
You'll find that methods are probably called just about what you expect them to be called. Here's some popular methods:
insert_one
insert_many
find_one
find_many
fine_one_and_update
fine_one_and_delete
update_one
update_many
delete_one
delete_many
Entire API: http://api.mongodb.com/python/current/api/index.html
Collection level docs: http://api.mongodb.com/python/current/api/pymongo/collection.html
Last updated
Was this helpful?