var mongoose = require('mongoose');
var db = mongoose.connect('mongodb://localhost/db001');
var UserSchema = new mongoose.Schema({
name: String,
num: Number
});
mongoose.model('User', UserSchema);
var User = mongoose.model('User');
var user1 = new User();
user1.name = 'hoge';
user1.num = 1111;
user1.save(function(err) {
if (err) { console.log(err); }
});
var user2 = new User({ name: 'piyo', num: 2222 });
user2.save();
var user3 = new User({ name: 'fuga', num: 3333 });
user3.save();
User.find({}, function(err, docs) {
for (var i=0, size=docs.length; i<size; ++i) {
console.log(docs[i].name + " " + docs[i].num);
}
});
User.find({}, function(err, docs) {
docs.forEach(function(doc,index){
console.log(index + ":" + doc.name + " " + doc.num);
});
});
User.findOne({'name':'piyo'}, function(err, doc){
console.log(doc.name);
});
User.remove({}, function(err){
if (err) { console.log(err); }
});