// mongoose 链接
var mongoose = require('mongoose');
var db = mongoose.createConnection('mongodb://127.0.0.1:27017/NodeJS');
// 链接错误
db.on('error', function(error) {
console.log(error);
});
// Schema 结构
var mongooseSchema = new mongoose.Schema({
username : {type : String, default : '匿名用户'},
title : {type : String},
content : {type : String},
time : {type : Date, default: Date.now},
age : {type : Number}
});
// 添加 mongoose 实例方法
mongooseSchema.methods.findbyusername = function(username, callback) {
return this.model('mongoose').find({username: username}, callback);
}
// 添加 mongoose 静态方法,静态方法在Model层就能使用
mongooseSchema.statics.findbytitle = function(title, callback) {
return this.model('mongoose').find({title: title}, callback);
}
// model
var mongooseModel = db.model('mongoose', mongooseSchema);
// 增加记录 基于 entity 操作
var doc = {username : 'emtity_demo_username', title : 'emtity_demo_title', content : 'emtity_demo_content'};
var mongooseEntity = new mongooseModel(doc);
mongooseEntity.save(function(error) {
if(error) {
console.log(error);
} else {
console.log('saved OK!');
}
// 关闭数据库链接
db.close();
});
// 增加记录 基于model操作
var doc = {username : 'model_demo_username', title : 'model_demo_title', content : 'model_demo_content'};
mongooseModel.create(doc, function(error){
if(error) {
console.log(error);
} else {
console.log('save ok');
}
// 关闭数据库链接
db.close();
});
// 修改记录
mongooseModel.update(conditions, update, options, callback);
var conditions = {username : 'model_demo_username'};
var update = {$set : {age : 27, title : 'model_demo_title_update'}};
var options = {upsert : true};
mongooseModel.update(conditions, update, options, function(error){
if(error) {
console.log(error);
} else {
console.log('update ok!');
}
//关闭数据库链接
db.close();
});
// 查询
// 基于实例方法的查询
var mongooseEntity = new mongooseModel({});
mongooseEntity.findbyusername('model_demo_username', function(error, result){
if(error) {
console.log(error);
} else {
console.log(result);
}
//关闭数据库链接
db.close();
});
// 基于静态方法的查询
mongooseModel.findbytitle('emtity_demo_title', function(error, result){
if(error) {
console.log(error);
} else {
console.log(result);
}
//关闭数据库链接
db.close();
});
// mongoose find
var criteria = {title : 'emtity_demo_title'}; // 查询条件
var fields = {title : 1, content : 1, time : 1}; // 待返回的字段
var options = {};
mongooseModel.find(criteria, fields, options, function(error, result){
if(error) {
console.log(error);
} else {
console.log(result);
}
//关闭数据库链接
db.close();
});
// 删除记录
var conditions = {username: 'emtity_demo_username'};
mongooseModel.remove(conditions, function(error){
if(error) {
console.log(error);
} else {
console.log('delete ok!');
}
//关闭数据库链接
db.close();
});
schema代码:
var mongoose = require('mongoose')
var MoviewSchema = new mongoose.Schema({
doctor: String,
title: String, //title : {type : String},
language: String,
country: String,
summary: String,
flash: String,
year: Number,
poster: String,
meta: {
createAt: {
type: Date,
default: Date.now()
},
updateAt: {
type: Date,
default: Date.now()
}
}
});
//为该模式添加一个方法
MoviewSchema.pre('save', function (next) {
if (this.isNew) {
this.meta.createAt = this.meta.updateAt = Date.now();
}
else {
this.meta.updateAt = Date.now();
}
next();
});
//静态方法 实例化或model后才能用
MoviewSchema.statics = {
fetch: function (callback) {
return this.find({}).sort('meta.updateAt').exec(callback)
},
findById: function (id, callback) {
return this.findOne({_id: id}).sort('meta.updateAt').exec(callback)
}
}
module.exports = MoviewSchema;
model代码:
var mongoose = require('mongoose')
var MovieSchema = require('../schemas/movie.js');
var Movie = mongoose.model('Movie', MovieSchema);//show collectios 就是movies 自动加个s
module.exports = Movie;