Mongoose是什麼?
Mongoose是MongoDB的一個物件模型工具,封裝了許多MongoDB對文件的的增刪改查等常用方法,讓NodeJS操作Mongodb資料庫變得更加靈活簡單。
在egg專案中如何使用?
1、安裝
npm i egg-mongoose --save
2、配置
在根目錄下的/config/plugin.js中配置外掛
exports.mongoose = { enable: true, package: 'egg-mongoose', };
3、連線資料庫
在根目錄下的/config/config.default.js增加配置,其中url為我們的資料庫地址,可透過環境變數來區分開發環境還是生產環境,並且確定是否使用使用者名稱密碼的資料庫
const prod = process.env.npm_config_server_prod;
mongoose: { client: { url: prod ? 'mongodb:eggadmin:123456@localhost:27017/DbName' : 'mongodb://127.0.0.1:27017/DbName', options: { useUnifiedTopology: true, }, }, },
4、配置與使用
(1)資料表配置
在app目錄下新建model資料夾,在model資料夾下新建JS檔案作為資料表的配置內容,下面以書籍表的配置為例
'use strict'; /** * @description: Mongoose book Schema, */ module.exports = app => { const mongoose = app.mongoose; const Schema = mongoose.Schema; const BookSchema = new Schema({ desc: { type: String }, /* 書籍描述 */ name: { type: String }, /* 書籍名稱 */ press: { type: String }, /* 出版社 */ author: { type: String }, /* 作者 */ image: { type: Array }, /* 書籍圖片列表*/ price: { type: String }, /* 價格 */ book_type: { /* 書籍分類id */ type: Schema.Types.ObjectId, ref: 'BookClassify', }, user: { /* 書籍釋出者id */ type: Schema.Types.ObjectId, ref: 'User', }, create_time: { type: String }, /* 建立時間 */ status: { type: String }, /* 狀態,1:待購買,2:已購買*/ look: { type: Number } /* 瀏覽數量 */ }); return mongoose.model('Book', BookSchema); };
可以看到我們可以透過Schema來定義表結構,可以指定欄位的型別及關聯,設定完欄位後就可以生成model了,這裡算是非常簡單的配置,更多配置方法可參考文件
(2)、使用mongoose方法
配置完資料表結構後,我們就可以再service層中呼叫mongoose的方法對文件進行增刪查改了,已書籍列表的處理邏輯為例子
async findbookList(data) { const { type, page, pageSize, desc, status, userId } = data; const searchVal = {} if (type) { searchVal.book_type = mongoose.Types.ObjectId(type) } if (status) { searchVal.status = status } if (userId) { searchVal.user = mongoose.Types.ObjectId(userId) } const search_term = { $or: [ { desc: { $regex: desc ? desc : '', $options: '$i' } }, { name: { $regex: desc ? desc : '', $options: '$i' } }, { author: { $regex: desc ? desc : '', $options: '$i' } }, { press: { $regex: desc ? desc : '', $options: '$i' } }, ], }; const totalNum = await this.ctx.model.Book.find(searchVal).and(search_term).countDocuments(); const result = await this.ctx.model.Book.find(searchVal) .populate({ path: 'user', select: { name: 1, image: 1 } }) .populate({ path: 'book_type' }) .and(search_term) .sort({ create_time: -1 }) .skip((parseInt(page) - 1) * parseInt(pageSize)) .limit(parseInt(pageSize)); return result ? { bean: { records: result, current: page, size: result.length, total: totalNum, }, ...app.config.msg.GET_SUCCESS } : app.config.msg.GET_ERR; }
可以看到,透過this.ctx.model.Book就可以獲取到Book的model並且可以呼叫mongoose需要的方法,例如populate、find、and、sort、skip、limit 等等。
5、egg-Mongoose常用的方法
增加資料
this.ctx.model.Book.create(data,callback);
其中data為json資料結構,callback為操作後的回撥函式
查詢資料
獲取所有資料,返回是一個陣列
this.ctx.model.Book.find()
獲取一個資料,返回是一個物件
this.ctx.model.Book.findOne()
條件查詢
this.ctx.model.Article.find(conditions,callback);
其中conditions為查詢的條件,callback為回撥函式
conditions有一下幾種情況:
具體資料:
this.ctx.model.Book.find ( {_id:5c4a19fb87ba4002a47ac4d, name: "射鵰英雄傳" } , callback) ;
條件查詢:
"$lt" 小於 "$lte" 小於等於 "$gt" 大於 "$gte" 大於等於 "$ne" 不等於 // 查詢價格大於100小於200的書籍陣列 this.ctx.model.Book.find({ "price": { $get:100 , $lte:200 });
或查詢 OR
"$in" 一個鍵對應多個值 "$nin" 同上取反, 一個鍵不對應指定值 "$or" 多個條件匹配, 可以巢狀 $in 使用 "$not" 同上取反, 查詢與特定模式不匹配的文件 this.ctx.model.Book.find({"name":{ $in: ["射鵰","倚天"]} );
刪除資料
this.ctx.model.Book.remove(conditions,callback);
更新資料
this.ctx.model.Book.update(conditions, update, callback)
conditions為條件,update是更新的值物件
排序
this.ctx.model.Book.sort({ create_time: -1 });
其中-1表示降序返回。 1表示升序返回
限制數量
this.ctx.model.Book.limit(number);
number表示限制的個數
跳過文件返回
this.ctx.model.Book.skip(number);
number表示跳過的個數,skip經常搭配limit實現分頁的功能
條件陣列and
在find後面可使用and對查詢結果進行進一步條件篩選,相當於並且的意思。
const search_term = { $or: [ { desc: { $regex: desc ? desc : '', $options: '$i' } }, { name: { $regex: desc ? desc : '', $options: '$i' } }, { author: { $regex: desc ? desc : '', $options: '$i' } }, { press: { $regex: desc ? desc : '', $options: '$i' } }, ], }; this.ctx.model.Book.find().and(search_term)
關聯查詢populate
// 在model中配置欄位時候指定關聯的表名,就可以透過populate來進行表的關聯查詢 user: { /* 書籍釋出者id */ type: Schema.Types.ObjectId, ref: 'User', }, this.ctx.model.Book.find() .populate({ path: 'user', select: { name: 1, image: 1 } })
聚合管道Aggregate
this.ctx.model.Template.aggregate([ { $match: { name } }, { $sort: { create_time: -1 } }, { $group: { _id: '$name', user_id: { $first: '$modifier' } } }, ]);
Mongoose聚合管道aggregate常用的操作有$project 、$match 、$group、$sort、$limit、$skip、$lookup 表關聯
批次操作bulkWrite
const template_list = await ctx.model.Template.aggregate([ { $sort: { create_time: -1 } }, { $group: { _id: '$name', template_id: { $first: '$_id' }, label: { $first: '$label' } } }, ]); const update_value = []; template_list.forEach(item => { if (!item.label) { update_value.push({ updateOne: { filter: { _id: item.template_id }, update: { label: '' }, }, }); } }); await ctx.model.Template.bulkWrite(update_value);
可以進行一系列批次增加、刪除、更新等操作。
mongoose還有非常多的方法可以提供給我的靈活使用,我們在使用的時候可以結合業務邏輯選擇合適的方法來提高我們運算元據庫的效率。在我們使用它之前可以認真的閱讀官方文件
[hongdian2012 ] Mongoose 在egg中的使用詳解已經有298次圍觀