Skip to content

Instantly share code, notes, and snippets.

@tfogo
Created October 8, 2017 04:58
Show Gist options
  • Select an option

  • Save tfogo/d40ea5c51a51cc33bb5c77183bdd869a to your computer and use it in GitHub Desktop.

Select an option

Save tfogo/d40ea5c51a51cc33bb5c77183bdd869a to your computer and use it in GitHub Desktop.
Example Flask and Mongo
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