article

Sunday, November 1, 2020

Flask with MongoDB how to setup and Create Read Update Delete (CRUD) methods

Flask with MongoDB how to setup and Create Read Update Delete (CRUD) methods

MongoDB is a popular database, but unlike other databases it’s classified as a NoSQL database program (MongoDB uses JSON-like documents with schema).

ModuleNotFoundError: No module named 'flask_mongoengine'

install module
(venv) C:\flaskmyproject>pip install flask-mongoengine

create file app.py
 
#app.py
import json
from flask import Flask, request, jsonify
from flask_mongoengine import MongoEngine #ModuleNotFoundError: No module named 'flask_mongoengine' = (venv) C:\flaskmyproject>pip install flask-mongoengine

app = Flask(__name__)
app.config['MONGODB_SETTINGS'] = {
    'db': 'dbtestmongo',
    'host': 'localhost',
    'port': 27017
}
db = MongoEngine()
db.init_app(app)

class User(db.Document):
    name = db.StringField()
    email = db.StringField()
    def to_json(self):
        return {"name": self.name,
                "email": self.email}

@app.route('/')
def query_records():
    name = 'tutorial101'
    user = User.objects(name=name).first()
    if not user:
        return jsonify({'error': 'data not found'})
    else:
        return jsonify(user.to_json())

@app.route('/add')
def create_record():
    user = User(name='Caite',
                email='caite@gmail.com')
    user.save()
    return jsonify(user.to_json())

@app.route('/update')
def update_record():
    name = 'tutorial101'
    user = User.objects(name=name).first()
    if not user:
        return jsonify({'error': 'data not found'})
    else:
        user.update(email='emailupdate@gmail.com')
    return jsonify(user.to_json())
	
@app.route('/delete')
def delete_record():
    name = 'tutorial101'
    user = User.objects(name=name).first()
    if not user:
        return jsonify({'error': 'data not found'})
    else:
        user.delete()
    return jsonify(user.to_json())
	
if __name__ == "__main__":
    app.run(debug=True)

Related Post