Created
April 1, 2011 05:15
-
-
Save mcantelon/897778 to your computer and use it in GitHub Desktop.
Mongoose example
This file contains 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
// from Horofox in #node.js | |
var mongoose = require('mongoose'); | |
var db = mongoose.connect('mongodb://localhost/mydb'); | |
function allowPosts(mongoose) { | |
var Schema = mongoose.Schema; | |
var Posts = new Schema({ | |
name : String, | |
subject: String, | |
comment : String, | |
password: String, | |
}); | |
mongoose.model('Post', Posts); | |
} | |
allowPosts(mongoose) | |
function createNewPost(){ | |
var Post = mongoose.model('Post'); | |
var post = new Post(); | |
post.subject='hshja'; | |
post.comment ='ahsjashjas'; | |
post.save(function(err){ | |
if(!err){ | |
console.log('Post saved.'); | |
} | |
}); | |
} | |
function findPosts(amount){ | |
var Post = mongoose.model('Post'); | |
console.log(Post.count({}, function(err,count) { | |
console.log('Count:' + count) | |
})) | |
Post.find({}).limit(amount).each(function(err,post){ | |
if(post!=null){ | |
console.log(post.subject); | |
} | |
}); | |
} | |
//createNewPost() | |
findPosts(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
terrible code