"Object Document Mapper"
$ npm install --save mongoose
var mongouri = 'mongodb://username:password@host:51625/db-name';
mongoose.connect(mongouri);
//handle connection error
mongoose.connection.on('error', function(e){
console.error(e);
});
var mongouri = process.env.MONGOURI; //set in process.env
//schema//
var dishSchema = mongoose.Schema({
name: String,
spicy: Boolean
});
var restaurantSchema = mongoose.Schema({
name: String,
founded: Number,
Dishes: [ dishSchema ]
});
//model//
var Restaurant = mongoose.model('Restaurant', restaurantSchema);
var restaurantSchema = mongoose.Schema({ /* name, founded, ...*/
Dishes: [ dishSchema ]
});
[
{
"_id": { "$oid": "56afe2b362e2946e89dd0e38" },
"name" : "Amici's",
"founded": 2010,
"Dishes": [
{
"name" : "tacos",
"spicy" : true 0},
"_id": { "$oid": "56afe2b362e2946e89dd0e3f" },
...
},
...
]
Nope!
In mongoose-restaurants
...
npm install
index.js
to match your environmentnode .
https://mongolab.com
/databases/{db}/collections/restaurants
Hints:
export MONGOURI=<yourmongouri>
https://mongolab.com/databases/{db}
restaurants
collection[
{
"_id": {
"$oid": "56afe2b362e2946e89dd0e25"
},
"name": "Taco Gong",
"founded": 2011,
"Dishes": [
{
"name": "Pancakes",
"spicy": false,
"_id": {
"$oid": "56afe2b362e2946e89dd0e2c"
}
},
...
Model.findOne
Model.findById
Model.find
Model.find([conditions, projection, options,] callback);
Post.find(handlePosts);
function handlePosts(err, posts){
//do something with posts Array
}
Post.find()
.exec()
.then(handlePosts)
.catch(function(e){ console.error(e); });
function handlePosts(posts){
//do something with posts Array
}
Connect the /restaurants/
route
Hints:
exec
)Model.find().exec()
.then(function(results){
//use results
});
restaurants
key:each restaurant in restaurants
li
a(href='/restaurants/' + restaurant.id)= restaurant.name
Post.find({ featured : true })
.exec().then(handler);
Post.find()
.where({featured : true })
.exec().then(handler);
Person
.find({ occupation: /host/ })
.where('name.last').equals('Ghost')
.where('age').gt(17).lt(66)
.where('likes').in(['walking', 'talking'])
.limit(10)
.sort('-occupation')
.exec().then(handler);
var query = Post.find(
{featured : true},
'title slug description' //fields to "project"
)
.exec().then(handler);
Post.find()
.where({featured : true })
.select('title slug description')
.exec().then(handler);
Included by default
[
{
"_id": { "$oid": "56afe2b362e2946e89dd0e38" },
"name" : "Amici's",
"founded": 2010,
"Dishes": [
{
"name" : "tacos",
"spicy" : true 0},
"_id": { "$oid": "56afe2b362e2946e89dd0e3f" },
...
},
...
]
Connect the /restaurants/:id
route
Hints:
req.params
findById
exec
!!)name
, founded
, Dishes
keysvar hd_post = new Post({
title : 'Are Hotdogs Sandwiches?'
});
hd_post.contents = postContents;
hd_post.save();
Post.create({
title : 'Are Hotdogs Sandwiches?',
contents : postContents
})
.then(function(hd_post){ ... });
Post
.findById(123)
.then(function(hd_post){
hd_post.title = 'The Hotdog Dilemma'
hd_post.save;
}).exec(callback);
var oldTitle = 'Are Hotdogs Sandwiches?';
var newTitle = 'The Hotdog Dilemma';
Post.findOneIdAndUpate(
{ title : oldTitle},
{ title : newTitle}
).exec(callback);
Connect the POST /restaurants/
route
Hints:
req.body
(already parsed! ☺)exec
create
or new Model
& save
THANKS FOR COMING!!!