zoukankan      html  css  js  c++  java
  • MongoDB多集合排序的一种实现

    需求

    假设有三个类型有所不同的表,saleorders、careorders、repairorders,表中有storeId信息,用于关联其所属的门店stores表,现在有个需求是要将这三个表集中展示在一个表格中展示。

    实现

    以下驱动使用mongoose实现,MongoDB版本大于3.6。

    
    var orders = Store.aggregate([
        {
            $match: {_id: storeId},
        },
        {
            $lookup: {
                from: "saleorders",
                let: {
                    storeId: storeId,
                },
                pipeline: [
                    {
                        $match: {
                            $expr: {
                                $eq: ['$storeId', '$$storeId'],
                            }
                        }
                    }
                ],
                as:'sales'
            }
        },
        {
            $lookup: {
                from: "careorders",
                let: {
                    storeId: storeId
                },
                pipeline: [
                    {
                        $match: {
                            $expr: {
                                $eq: ['$storeId', '$$storeId']
                            }
                        }
                    }
                
                ],
                as: 'cares'
            }
        },
        {
            $lookup: {
                from: "repairorders",
                let: {
                    storeId: storeId
                },
                pipeline: [
                    {
                        $match: {
                            $expr: {
                                $eq: ['$storeId', '$$storeId']
                            }
                        }
                    }
                
                ],
                as: 'repairs'
            }
        },
        {
            $addFields: {
                "repairs.orderType": '1',
                "sales.orderType": '2',
                "cares.orderType": '3',
            }
        },
        {
            $project: {
                _id: 0,
                items: {
                    $concatArrays: ['$repairs', '$cares', '$sales']
                }
            }
        },
        {$unwind: '$items'},
        {
            $replaceRoot: {
                newRoot: '$items'
            }
        },
        {$sort: {_id: -1 }},
    ])
    

    这种实现的重点是使用一个排序集合之外的文档来关联这三个集合以及使用操作符$concatArrays来组合多个数组。常规的思路是,你要哪几个集合,就用那些集合进行关联,然后$unwind操作,但这种操作无法实现预期目的,因为$lookup是对每个文档进行对外关联。上述写法的思路是,假设几个集合的文档都集中在一个文档中的三个字段上,那么我们可以在单个集合上对数组合并排序。当然,前提第一步必须能找到一定会存在的唯一文档来实现关联,否则下面的步骤要不就关联不了,要不就关联多次,鉴于上面的操作是针对单个storeId的,所以必然能从stores集合中找到唯一的store文档进行关联。

    当然,现实情况应该尽量避免这种方式来设计集合, 对于类型相似的文档应该放在同一个集合中,而且这种查询方式很可能出现聚合的中间步骤产生的数据量超过MongoDB限制(默认100M)的问题,应当使用allowDiskUse: true设置。

  • 相关阅读:
    Codeforces 678E 状压DP
    Codeforces 667C DP
    POJ 3017 DP + 单调队列 + 堆
    Codeforces 1154F (DP)
    Codeforces 1154G 枚举
    Codeforces 1153D 树形DP
    Codeforces 1109E 线段树
    Codeforces 1109C 线段树
    Codeforces 1109D (树的计数问题)
    async/await
  • 原文地址:https://www.cnblogs.com/zhangjpn/p/9663703.html
Copyright © 2011-2022 走看看