mongoose populate 返回 指定 字段
生活随笔
收集整理的這篇文章主要介紹了
mongoose populate 返回 指定 字段
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、mongoose中一個數據模型Product(商品)關聯另外一個數據模型Brand(品牌)需要使用ref,關聯查詢使用populate
Product模型
Brand模型
new mongoose.Schema({// 品牌name: { type: String, required: true, validate: /\S+/ },// 品牌圖片img: { type: String, required: true, validate: /\S+/ }...2、查詢使用populate
// 過濾條件 const options = {sort: { _id: -1 },page: Number(page),limit: Number(size),// populate: ['brand', 'tag'],populate: [{ path: 'brand', select: 'name' }, 'tag'],select: '-password -content' };// 查詢參數const keywordReg = new RegExp(keyword)const query = {"$or": [{ 'name': keywordReg },{ 'slug': keywordReg },{ 'description': keywordReg }]}const ones = await Product.paginate(query, options)3、注意上段代碼中
populate: ['brand', 'tag'],brand所有字段都返回
populate: [{ path: 'brand', select: 'name' }, 'tag'],brand返回name字段
總結
以上是生活随笔為你收集整理的mongoose populate 返回 指定 字段的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 唐宇迪学习笔记6:线性回归算法原理推导
- 下一篇: Python成长之路【第七篇】:Pyth