MongoDB - Non-relational databases¶
Definition¶
Non-relational databases have been established as the favorite option for the Internet of Things paradigm based on their flexibility over their data structure and a faster data retrieve when compared with relational databases. Therefore, non-relational database presents qualities more compatibles with the expected needs of the smart bio-manufacturing, and it is then the data storage strategy selected for our database.
MongoDB is currently the most popular example of non-relational database program. Some examples of software using MongoDB are SEGA, BARCLAYS, or the tax platform for the UK government, etc. MongoDB organizes its data in documents that subsequently are organized in collections, usually with common fields between documents. Those documents characterize by be available to store their data in a JSON data format.
How to…¶
It is a simple script using Flask in order to create a REST API with a mongo database to get and post data.
from flask import Flask, render_template, request, Response
from flask_pymongo import PyMongo
import json
from werkzeug.security import generate_password_hash, check_password_hash #Cifrar y descifrar contraseñas de usuario
from bson import json_util
from bson.objectid import ObjectId
app= Flask(__name__)
app.config['MONGO_URI'] = 'mongodb://localhost:27017/minerva'
mongo = PyMongo(app)
@app.route('/models', methods=['POST'])
def create_data():
model_name = request.json['model_name']
model = request.json['model']
if model_name and model:
id = mongo.db.models.insert(
{'model_name': model_name, 'model': model}
)
response = {
'id' : str(id),
'model_name' : model_name,
'model' : model
}
return response
else:
return message
@app.route('/models', methods=['GET'])
def get_models():
models = mongo.db.models.find()
response = json_util.dumps(models)
return Response(response, mimetype="application/json")
@app.route('/models/<id>', methods = ['GET'])
def get_model(id):
model = mongo.db.models.find_one({'_id': ObjectId(id)})
response = json_util.dumps(model)
return Response(response, mimetype="application/json")
@app.errorhandler(404)
def not_found(error=None):
message = {
'message': 'Resource Not Found ' + request.url,
'status': 404
}
return message
if __name__ == "__main__":
app.run(debug=True)