Mongoose就是一套操做MongoDB数据库的接口,而Egg中有对应的插件egg-mongoose。java
$ npm install egg-mongoose --save
复制代码
改变Egg项目中的配置文件{workplace}/config/plugin.js中来启用 egg-mongoose 插件:web
exports.mongoose = {
enable: true,
package: 'egg-mongoose',
};
复制代码
在Egg项目中的配置文件{workplace}/config/default.js配置项config添加属性正则表达式
config.mongoose = {
url: process.env.EGG_MONGODB_URL || 'mongodb://127.0.0.1/website',
options: {
server: {
poolSize: 40,
},
},
};
复制代码
在{workplace}/app/model/article.js定义数据表mongodb
'use strict';
module.exports = app => {
const mongoose = app.mongoose;
const Schema = mongoose.Schema;
const PostSchema = new Schema({
wid: {
type: String,
},
release: {
type: Boolean,
},
sort: {
type: Number,
},
img: {
type: String,
},
abstract: {
type: String,
},
text: {
type: String,
},
isSetTop: {
type: Number,
},
title: {
type: String,
},
keywords: {
type: String,
},
describe: {
type: String,
},
updateTime: {
type: Date,
},
num: {
type: Number,
},
uid: {
type: String,
},
editors: {
type: String,
},
disable: {
type: Boolean,
},
columnId: {
type: Schema.Types.ObjectId,
},
});
return mongoose.model('Article', PostSchema);
};
复制代码
备注:其中type表示字段类型,Mongoose 有如下几种类型Number(数字),String(字符串),Boolean(布尔值),ObjectId(对象ID),Array(数组),Object(对象),Date(日期)。。。数据库
this.ctx.model.Article.create(post,callback);
复制代码
备注:其中post为json数据结构,callback为操做后的回调函数npm
this.ctx.model.Article.find()
复制代码
this.ctx.model.Article.findOne()
复制代码
this.ctx.model.Article.find(conditions,callback);
复制代码
condition有如下几种类型json
this.ctx.model.Article.find({_id:5c4a819fb87ba4002a47bc4f,title:"123"},callback);
复制代码
"$lt" 小于
"$lte" 小于等于
"$gt" 大于
"$gte" 大于等于
"$ne" 不等于
复制代码
this.ctx.model.Article.find({“sort”:{ $get:18 , $lte:30 });
复制代码
"$in" 一个键对应多个值
"$nin" 同上取反, 一个键不对应指定值
"$or" 多个条件匹配, 能够嵌套 $in 使用
"$not" 同上取反, 查询与特定模式不匹配的文档
复制代码
this.ctx.model.Article.find({"title":{ $in:[20,21,22."haha"]} );
复制代码
this.ctx.model.Article.find({"$or" : [ {"age":18} , {"name":"wxw"} ] });
复制代码
"$exists"
条件断定)this.ctx.model.Article.find({name: {$exists: true}},function(error,docs){
//返回Article表中全部存在name属性的结果
});
复制代码
this.ctx.model.Article.find({telephone: {$exists: false}},function(error,docs){
//返回Article表中全部不存在telephone属性的结果
});
复制代码
MongoDb 是使用 Prel兼容的正则表达式库来匹配正则表达式数组
this.ctx.model.Article.find( {"name" : /joe/i } );
复制代码
this.ctx.model.Article.find({"array":10} );
复制代码
this.ctx.model.Article.find({"array[5]":10} );
复制代码
this.ctx.model.Article.find({"array":[5,10]});
复制代码
this.ctx.model.Article.find({"array":{$size : 3} });
复制代码
this.ctx.model.Article.find({"array":{$slice : 10} });
复制代码
this.ctx.model.Article.find({"array":{$slice : [5,10]} });
复制代码
用它能够执行任意javacript语句做为查询的一部分,若是回调函数返回 true 文档就做为结果的一部分返回bash
this.ctx.model.Article.find( {"$where" : "this.x + this.y === 10" } );
this.ctx.model.Article.find( {"$where" : " function(){ return this.x + this.y ===10; } " } )
复制代码
this.ctx.model.Article.remove(conditions,callback);
复制代码
备注:conditions为查询条件,与查询数据介绍的同样,eg:{ _id:5c4a819fb87ba4002a47bc4f },找到_id为5c4a819fb87ba4002a47bc4f的数据,callback为操做成功后的回调函数数据结构
this.ctx.model.Article.update(conditions, update, callback)
复制代码
备注:conditions与查询数据中介绍的同样
let post = {
wid: '5c492c57acbe363fd4824446',
column: [ '新闻' ],
titleHead: '',
img: '',
isAbstract: 'false',
}
this.ctx.model.Article.update({ _id: '5c4a819fb87ba4002a47bc4f ' }, post)
复制代码
"$inc"
增减修改器,只对数字有效this.ctx.model.Article.update({"age":22}, {$inc:{"age":1} } );
复制代码
'$set'
指定一个键的值,这个键不存在就建立它.能够是任何MondoDB支持的类型.this.ctx.model.Article.update({ _id:5c4a819fb87ba4002a47bc4f }, { $set: { isDelete: true } });
复制代码
"$unset"
同上取反,删除一个键this.ctx.model.Article.update({age:22}, {$unset:{age:18} } );
复制代码
'$push'
给一个键push一个数组成员,键不存在会建立,对数组有效this.ctx.model.Article.update({name:'wxw'}, {$push:{array:10} } );
复制代码
'$addToSet'
向数组中添加一个元素,若是存在就不添加this.ctx.model.Article.update({name:'wxw'},{$addToSet:{array:10} } );
复制代码
'$each'
遍历数组和 $push 修改器配合能够插入多个值this.ctx.model.Article.update({name:'wxw'}, {$push:{array:{$each: [1,2,3,4,5]}} } );
复制代码
'$pop'
向数组中尾部删除一个元素this.ctx.model.Article.update({name:'wxw'}, {$pop:{array:1} } );
复制代码
'$pull'
向数组中删除指定元素this.ctx.model.Article.update({name:'wxw'}, {$pull:{array:10} });
复制代码
this.ctx.model.Article.sort({ isSetTop: -1, sort: 1, editTime: -1 });
复制代码
备注:键对应数据中的键名,值表明排序方向,1 升序, -1降序。
this.ctx.model.Article.limit(3);
复制代码
this.ctx.model.Article.skip(3);
复制代码
附:综合使用最后三个方法进行分页查询
this.ctx.model.Article.find({ _id:5c4a819fb87ba4002a47bc4f }).skip(pageSize * (pageNum - 1)).limit(parseInt(pageSize)).sort({ isSetTop: -1, sort: 1, editTime: -1 });
复制代码