-
-
Save azamsharp/5ec759765302d8dd4fca08f922a36152 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const mongoose = require('mongoose') | |
let reviewSchema = mongoose.Schema({ | |
title : String, | |
description :String, | |
dishId : mongoose.Schema.ObjectId | |
}) | |
const Review = mongoose.model('Review',reviewSchema) | |
module.exports = Review | |
const mongoose = require('mongoose') | |
const Review = require('./reviewSchema') | |
// defining the structore of your document | |
let dishSchema = mongoose.Schema({ | |
name : String, | |
price :Number, | |
imageURL :String | |
}) | |
// convert the schema into a model class which you can use in your code | |
const Dish = mongoose.model('Dish',dishSchema) | |
module.exports = Dish | |
Dish.find({ reviews: { $type: "array", $not: { $size: 0 } } }, | |
function(error,dishes){ | |
console.log(dishes) | |
}) | |
const express = require('express') | |
const mongoose = require('mongoose') | |
const Dish = require('./schemas/dishSchema') | |
const Review = require('./schemas/reviewSchema') | |
const app = express() | |
// connect to the database nailaskitchen | |
// if the database does not exist then it will create it | |
// make sure the mongodb server is running using mongod at terminal | |
mongoose.connect('mongodb://localhost/nailaskitchen') | |
// get the database connection object | |
const db = mongoose.connection | |
// create a dish | |
let dish = new Dish({ name : "Noodles with reviews", price : 10}) | |
Review.findById('5b3a782cbe60902199755646',function(error,review){ | |
Dish.findById(review.dishId,function(error,dish){ | |
console.log(dish) | |
}) | |
}) | |
// find the dish based on the dishId in review | |
/* | |
dish.save(function(error,newDish){ | |
// create a review | |
let review = new Review({ title : 'new revie', description : "description", dishId : dish._id }) | |
review.save(function(error,newReview){ | |
console.log(newReview) | |
}) | |
}) */ | |
// create a review | |
// get all dishes where name contains Chicken | |
/* | |
Dish.find({"name" : /.*chicken.*///i},function(error,dishes){ | |
// console.log(dishes) | |
//}) | |
// get all dishes by reviews | |
/* | |
Dish.find({ | |
"reviews.length" : { $gt : 0 } | |
},function(error,dishes){ | |
console.log(dishes) | |
}) */ | |
//db.users.findOne({"username" : {$regex : ".*son.*"}}); | |
/* | |
dish.save(function(error,newDish){ | |
console.log(newDish) | |
}) */ | |
/* | |
Dish.find(function(error,dishes){ | |
dishes.forEach(function(dish){ | |
console.log(dish.name) | |
console.log(dish.reviews) | |
}) | |
}) */ | |
/* | |
dish.save(function(error,newDish){ | |
console.log(newDish) | |
})*/ | |
// get all the documents in the dishes collection | |
app.get('/dishes/:dishId',function(req,res){ | |
let dishId = req.params.dishId | |
Dish.findById(dishId,function(error,dish){ | |
res.json(dish) | |
}) | |
}) | |
app.get('/dishes',function(req,res){ | |
Dish.find(function(error,dishes){ | |
res.json(dishes) | |
}) | |
}) | |
// insert a new dish | |
/* | |
let dish = new Dish({ name : "Cheese Cake", price : 5, imageURL : "imageURLOfCheeseCake" }) | |
dish.save(function(error,newDish){ | |
if(error != null) { | |
console.error(error) | |
return | |
} | |
console.log(newDish) | |
}) */ | |
app.listen(3000, () => console.log('Example app listening on port 3000!')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment