Thursday, January 17, 2019

MongoDB: Find One method


Code
===================================
from flask import Flask
from flask import jsonify
from flask_pymongo import PyMongo

app = Flask(__name__)

app.config['MONGO_DBNAME'] = 'esawal'
app.config['MONGO_URI'] = 'mongodb://localhost:27017/esawal'

mongo = PyMongo(app)

# FIND ONE
@app.route('/<string:name>', methods=['GET'])
def get_one_star(name):
  star = mongo.db["customers"]
  s = star.find_one({'name' : name})
  output = []
  if s:
    output.append({'address': s['address'], 'name': s['name']})
  else:
    output = "No such name"
  return jsonify({'result' : output})


if __name__ == '__main__':
    app.run(debug=True)
===================================

OUTPUT

===================================

{
  "result": [
    {
      "address": "Apple st 652",
      "name": "Amy"
    }
  ]
}

===================================




22

No comments:

Post a Comment