账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    mongoose 怎样联查完成数据然后,根据联查的字段分类聚合?
    33
    0

    问题描述

    mongoose 怎样联查完成数据然后,根据联查的字段分类聚合?

    我现在有两张表 一个产品表 Product,一个产品类别表 Category,产品表的option关联类别表;
    产品表
    const mongoose  =  require('../index')
    const  Schema = mongoose.Schema;
    const ObjectId = Schema.Types.ObjectId
    const newSchema = new Schema({
      name:String,
      dec:String,
      tags:Array,
      originalPrice:Number,
      presentPrice:Number,
      proImg:Array,
      detailImg:Array,
      option:{
        type:ObjectId,
        ref:'Category'
      },
    })
    module.exports = mongoose.model('Product',newSchema,'product')
    类别表
    const mongoose  =  require('../index')
    const  Schema = mongoose.Schema;
    const newSchema = new Schema({
      name:String,
      isShow:{
        type:Boolean,
        default:true,
        },
    })
    module.exports = mongoose.model('Category',newSchema,'category')
    普通联查 const data = await dbProduct.find().populate('option')
    const data = {
        "code": 1,
        "message": "",
        "data": [
            {
                "tags": [
                    "测试标签"
                ],
                "proImg": [
                    "img-1531487107064.png"
                ],
                "detailImg": [
                    "img-1531487109047.jpg"
                ],
                "_id": "5b48ae6f8414a404d8756318",
                "presentPrice": 888,
                "name": "商品名称",
                "dec": "测试商品描述",
                "originalPrice": 1000,
                "__v": 0,
                "option": {                         //关联到的表
                    "isShow": true,
                    "_id": "5b4b0230b667d730707e0987",
                    "name": "时尚彩妆",
                    "__v": 0
                }
            },
    
    
        ]
    }

    我现在想通过聚合aggregate + 表的联查 populate 让我查到的数据排列成下面这种格式:

    const data = [
        {
            "title": "时尚彩妆", // 类别 
            "subTitle": "Fashion Make-up",
            "list": [   //商品列表
                {
                    "productName": "丝绒唇釉雾",  //商品名称 ...
                    "branchName": "香奈儿Chanel",
                    "img": "/caizhuang/1.jpg"
                },
                {
                    "productName": "邂逅柔情清新活力淡香水",
                    "branchName": "香奈儿Chanel",
                    "img": "/caizhuang/2.jpg"
                },
    
            ]
        },
    ]

    问题出现的环境背景及自己尝试过哪些方法

    我现在通过 特别 lowforEach,遍历两张查询出来的数据做处理。

    router.get('/homeLists/index', async ctx => {
      // 2018年7月15日17:38:12 聚合查询还用不溜 暂时不用他,直接遍历处理商品
      // const lists = await dbProduct.aggregate().group({_id:'$option'}).count('productNumber')
      // console.log('lists :', lists);
      const categoryLists = await dbCategory.find()
      const productLists = await dbProduct.find()
      let tempList = []
      categoryLists.forEach(cItem => { //遍历类目表
        let categoryInfo = {
          title: cItem.name,
          subTitle: 'Fashion Make-up', //写死 不用管
          list: []
        }
        productLists.forEach(pItem => {
          if (pItem.option.toString() == cItem._id.toString()) { //商品的类目_id 和 类目的_id作对比 相同的 加入相关的类目
            console.log('true :', true);
            let productInfo = {
              productName: pItem.name,
              proDec: pItem.dec,
              img: pItem.proImg[0]
            }
            categoryInfo.list.push(productInfo)
          }
        });
        tempList.push(categoryInfo) //插入遍历的类目信息
      })
    
      ctx.body = tempList
    })

    我希望能用上mongoose 的聚合功能来一次性实现!!

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 擱湹牟蕼 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部