Created
October 8, 2017 04:58
-
-
Save tfogo/d40ea5c51a51cc33bb5c77183bdd869a to your computer and use it in GitHub Desktop.
Example Flask and Mongo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from flask import Flask | |
| from pymongo import MongoClient | |
| from bson.json_util import dumps | |
| app = Flask(__name__) | |
| db = MongoClient('mongodb://<dbuser>:<dbpass>@localhost:27017/mydb')['mydb'] | |
| @app.route('/') | |
| def index(): | |
| return 'Hello, World!' | |
| @app.route('/contacts') | |
| def contacts(): | |
| return dumps(db.collection.find({})) | |
| @app.route('/random') | |
| def random(): | |
| return dumps(db.collection.aggregate({ '$sample': { 'size': 1 } })) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment