Skip to content

Instantly share code, notes, and snippets.

@mcantelon
Created April 1, 2011 05:15
Show Gist options
  • Save mcantelon/897778 to your computer and use it in GitHub Desktop.
Save mcantelon/897778 to your computer and use it in GitHub Desktop.
Mongoose example
// 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)
@alsotang
Copy link

terrible code

@davidleureka
Copy link

Doesn't work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment