NoSql
NoSql
Use Db
Db.CreateCollection("restauranats");
db.restaurants.insertOne({
"name": "Le Gourmet",
"borough": "Paris",
"cuisine": "French",
"address": {
"street": "10 Rue de Rivoli",
"zipcode": "75001"
},
"grades": [
{ "date": ISODate("2023-01-01T00:00:00Z"), "grade": "A", "score": 18 }
]
});
Insertion multiple
db.restaurants.insertMany([
{
"name": "Pizza Napoli",
Untitled 1
"borough": "Naples",
"cuisine": "Italian",
"address": { "street": "Piazza Garibaldi", "zipcode": "80142" },
"grades": [{ "date": ISODate("2023-02-01T00:00:00Z"), "grade": "B", "sc
ore": 15 }]
},
{
"name": "Sushi Tokyo",
"borough": "Tokyo",
"cuisine": "Japanese",
"address": { "street": "Shibuya", "zipcode": "150-0002" },
"grades": [{ "date": ISODate("2023-03-01T00:00:00Z"), "grade": "A", "sc
ore": 20 }]
}
]);
db.restaurants.find({});
Untitled 2
Requête avec plusieurs critères
db.restaurants.updateOne(
{ "name": "Le Gourmet" },
{ "$set": { "cuisine": "Modern French" } }
);
db.restaurants.updateMany(
{ "cuisine": "Italian" },
{ "$set": { "cuisine": "Authentic Italian" } }
);
Untitled 3
db.restaurants.deleteOne({ "name": "Le Gourmet" });
app = Flask(__name__)
# Connexion MongoDB
app.config["MONGO_URI"] = "mongodb://localhost:27017/restaurantsDB"
mongo = PyMongo(app)
app.route('/restaurants', methods=['POST'])
def add_restaurant():
data = request.json
Untitled 4
if not data.get("name") or not data.get("borough") or not data.get("cuisin
e"):
return jsonify({"error": "Missing required fields"}), 400
restaurant_id = mongo.db.restaurants.insert_one(data).inserted_id
return jsonify({"message": "Restaurant added", "id": str(restaurant_id)}), 20
1
Exemple de requête
POST http://localhost:5000/restaurants
Content-Type: application/json
{
"name": "Sushi Place",
"borough": "Tokyo",
"cuisine": "Japanese",
"address": {
"street": "Shinjuku",
"zipcode": "160-0022"
}
}
app.route('/restaurants', methods=['GET'])
def get_restaurants():
restaurants = list(mongo.db.restaurants.find({}, {"_id": 0}))
return jsonify(restaurants), 200
Exemple de requête
Untitled 5
GET http://localhost:5000/restaurants
@app.route('/restaurants/<id>', methods=['GET'])
def get_restaurant(id):
restaurant = mongo.db.restaurants.find_one({"_id": ObjectId(id)})
if not restaurant:
return jsonify({"error": "Restaurant not found"}), 404
return jsonify(restaurant), 200
Exemple de requête
GET http://localhost:5000/restaurants/65f2b34ae3c10c233b789012
python
CopierModifier
@app.route('/restaurants/<id>', methods=['PUT'])
def update_restaurant(id):
data = request.json
updated_data = {key: value for key, value in data.items() if key != "_id"}
Untitled 6
Exemple de requête
http
CopierModifier
PUT http://localhost:5000/restaurants/65f2b34ae3c10c233b789012
Content-Type: application/json
{
"cuisine": "Traditional Japanese"
}
python
CopierModifier
@app.route('/restaurants/<id>', methods=['DELETE'])
def delete_restaurant(id):
result = mongo.db.restaurants.delete_one({"_id": ObjectId(id)})
if result.deleted_count == 0:
return jsonify({"error": "Restaurant not found"}), 404
return jsonify({"message": "Restaurant deleted"}), 200
Exemple de requête
http
DELETE http://localhost:5000/restaurants/65f2b34ae3c10c233b789012
3. Requêtes avancées
Untitled 7
Rechercher des restaurants avec filtres dynamiques (GET)
@app.route('/restaurants/search', methods=['GET'])
def search_restaurants():
name = request.args.get("name")
borough = request.args.get("borough")
query = {}
if name:
query["name"] = {"$regex": name, "$options": "i"} # Insensible à la cass
e
if borough:
query["borough"] = borough
Exemple de requête
GET http://localhost:5000/restaurants/search?name=pizza&borough=Brookly
n
if __name__ == '__main__':
app.run(debug=True)
Untitled 8