article

Sunday, October 25, 2020

Download and install mongoDB and connect to python flask using PyMongo module

Download and install mongoDB and connect to python flask using PyMongo module

Download and install mongoDB on Windows 10 and connect to python flask using PyMongo module Source Code : In this video I am going to show How to install MongoDB on Windows 10 operating system. How to Install MongoDB Community Edition on Windows. How to Install MongoDB Compass tools How to Install python PyMongo module How to connect MongoDB to python flask pymongo and check the result MongoDB is one of the most used, open-source document database, and NoSQL database. show dbs-Print a list of all databases on the server. use {db} - Switch current database to {db} show collections-Print a list of all collections for current database. db.collection.find()-Find all documents in the collection and returns a cursor. Create database use dbtestmongo insert data db.user.insert({ "username":"tutorial101" }) show dbs show collections; db.user.find() set invironment C:\Program Files\MongoDB\Server\4.4\bin


(venv)$ pip install Flask-PyMongo
 
#app.py
from flask import Flask, jsonify
from flask_pymongo import PyMongo #(venv)$ pip install Flask-PyMongo

app = Flask(__name__)

app.config["MONGO_URI"] = "mongodb://localhost:27017/dbtestmongo"
mongo = PyMongo(app)

@app.route("/")
def index():
    online_users = mongo.db.user.find({"username": "testusername"})
    output = []
    for s in online_users:
      output.append({'User Name' : s['username']})
    return jsonify({'result' : output})
		
if __name__ == "__main__":
    app.run(debug=True)

Related Post