核心提示:Mongoose学习笔记重点记录基本操作 (详见 #a2)var Promise = require(bluebird)var mongoose = Promise.promisifyAll(requ...
Mongoose学习笔记
重点记录
基本操作 (详见 #a2)
var Promise = require('bluebird')
var mongoose = Promise.promisifyAll(require('mongoose'))// (详见 #b2)
// 连接数据库
mongoose.connect('127.0.0.1:27017/foobar')
mongoose.Promise = Promise// (详见 #b1)
var Schema = mongoose.Schema
// 定义 Schema
var blogSchema = new Schema({
index: Number,
sex: String,
})
// 编译 model
var Blog = mongoose.model('Blog', blogSchema)
// 新建虚拟属性(不保存到数据库)
var blog = new Blog({
index: 100,
})
// 保存到数据库
blog.saveAsync().then(function(data) {
console.log(data)
})
// 直接新建(保存到数据库)
Blog.createAsync({index: 200}).then(function(data) {
console.log(data)
})
// 查找
Blog.find({index: 0}, function(err, data) {
console.log(err)
for(var i=0; i自定义验证
...
var Schema = mongoose.Schema
// 定义 Schema
var blogSchema = new Schema({
index: {
type: Number,
min: [5, '太小了'],// 最小值
max: 10,// 最大值
},
age: {
type: {},// 使用自定义验证时,这里最好使用{}(这样可以去掉原来的类型验证)
validate: {
validator: function(value) {// 自定义验证
return /^\d{2}$/.test(value)
},
message: '{VALUE}只能是两位数',
},
required: [true, '必须填入此项'],// 必须值和错误信息
},
sex: {
type: String,
required: true,// 必须值
enum: {
values: ['male', 'female'],// 枚举值
message: '{PATH}的值不能是{VALUE}',// 自定义错误提示 {PATH}-当前被验证键 {VALUE}-已经被确定是错误的值
},
},
date: Date,
})
// 新建时将自动对数据进行验证
Blog.create({index: 5, sex: 'male', age: 12}, function(err, data) {
if(err) console.log(err.message)
console.log(data)
})
// 更新时默认是不验证的
Blog.update({age: 12}, {sex: 'female2'}, {runValidators: true}, function(err, data) {// runValidators-true 开启验证
if(err) console.log(err.message, 1)
console.log(data, 2)
})
方法 Promise 化 (详见 #b1)
var Promise = require('bluebird')
var mongoose = Promise.promisifyAll(require('mongoose'))
...
Blog.findAsync({index: 0}).then(function(data) {
console.log(data)
}).catch(function(err) {
console.log(err)
})
数据库时间格式化
数据库默认存储的时间格式,如:ISODate("2017-05-28T03:40:53.190Z") (详见 #a4)
展示到前端的时候,需要格式化处理一下
var moment = require('moment')
...
// 查找
Blog.find({}, function(err, data) {
for(var i=0; ischema.pre
...
var schema = new Schema({.
intro: String,
}, {
timestamps: true,// 自动加updatedAt和createdAt字段,且下次更新文档时,updatedAt字段会自动更新
})
// 保存时调用
schema.pre('save', function(next, done) {
console.log(this.isNew)
next()
})
// 更新时调用
schema.pre('update', function(next, done) {
console.log(this.getUpdate())
var intro = this.getUpdate().$set.intro
if(intro) {
this.update({}, { intro: intro +'something' })
}
console.log(this.getUpdate())
next()
})
...
populate(关联查询) (详见 #c)
movie.js(不关联任何集合)
...
var schema = new Schema({
title: String,
intro: String,
})
module.exports = mongoose.model('Movie', schema)
user.js(关联Movie集合)
...
var schema = new Schema({
movie: {
type: Schema.Types.ObjectId, ref: 'Movie',// 关联Movie集合,且只有一个,可以用$addToSet来更新此字段
},
username: {
type: String,
unique: true,
},
password: {
type: String,
},
})
module.exports = mongoose.model('User', schema)
article.js(关联User集合)
...
var schema = new Schema({
user: [{
type: Schema.Types.ObjectId,
ref: 'User',
}],// 关联User集合,且可以有多个,可以用$push来更新此字段
title: String,
content: String,
})
schema.statics = {
findByTitle: function(title) {
return this
.find({title: title})
.populate([// 因为User集合里面关联了Movie集合,所以populate里面嵌套populate可以用来设置最里面的那层集合所要显示的字段信息
{path: 'user', select: '-_id movie username' populate: {path: 'movie'}}
])
}
}
module.exports = mongoose.model('Article', schema)
app.js
...
// 调用这个接口,cmd中查看效果
router.all('/findByTitle', function(req, res) {
Article.findByTitle(req.body.title)
.then(function(data) {
console.log(data)
})
.catch(function(err) {
console.log(err)
})
})
...
参考文章
Mongoose [#a]
Mongoose 英文文档 [#1] Mongoose 中文文档 [#2] Mongoose 的exec(cb)具体什么意思啊? [#3] mongo-日期类型(2) [#4]
Mongoose Promise [#b]
“mpromise (mongoose’s default promise library) is deprecated” error when testing [duplicate] [#1] Promise.promisifyAll [#2]
populate(关联查询) [#c]
Mongoose Populate 基本使用 [#1]


