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 });
複製代碼