• MongoDB 聚合查询 $unwind


    在aggregate中,常常会遇到一些字段属性是数组对象,然后又需要对这些数组对象进行统计。
    这时候就需要用到$unwind操作符。这是一个常用的,又容易被忽略的一个操作。

    定义

    • field 版

      { $unwind: <field path> }
      
    • document版

      {
        $unwind:
          {
            path: <field path>,
            includeArrayIndex: <string>,
            preserveNullAndEmptyArrays: <boolean>
          }
      }
      
    1. 你要打散的字段
    2. includeArrayIndex,分配一个存该数组索引的字段
    3. preserveNullAndEmptyArrays,是否输出空内容。

    1、插入数据

    MongoDB Enterprise > db.mytestcol.insert({user_id:"A_id",bonus:[{ type:"a" ,amount:1000 },{ type:"b" ,amount:2000 },{ type:"b" ,amount:3000 }]}

    MongoDB Enterprise > db.mytestcol.insert({user_id:"B_id",bonus:[{ type:"a" ,amount:1000 },{ type:"b" ,amount:2000 },{ type:"b" ,amount:3000 }]})

    2、查看数据

    1)普通查看

    MongoDB Enterprise > db.mytestcol.find().pretty()
    {
            "_id" : ObjectId("5e1d66b240741afd9cfdee9d"),
            "user_id" : "B_id",
            "bonus" : [
                    {
                            "type" : "a",
                            "amount" : 1000
                    },
                    {
                            "type" : "b",
                            "amount" : 2000
                    },
                    {
                            "type" : "b",
                            "amount" : 3000
                    }
            ]
    }
    {
            "_id" : ObjectId("5e1d66d540741afd9cfdee9e"),
            "user_id" : "A_id",
            "bonus" : [
                    {
                            "type" : "a",
                            "amount" : 1000
                    },
                    {
                            "type" : "b",
                            "amount" : 2000
                    },
                    {
                            "type" : "b",
                            "amount" : 3000
                    }
            ]
    }

    2)$unwind查看,将数组拆开

    MongoDB Enterprise > db.mytestcol.aggregate([{"$unwind":"$bonus"}])
    { "_id" : ObjectId("5e1d66b240741afd9cfdee9d"), "user_id" : "B_id", "bonus" : { "type" : "a", "amount" : 1000 } }
    { "_id" : ObjectId("5e1d66b240741afd9cfdee9d"), "user_id" : "B_id", "bonus" : { "type" : "b", "amount" : 2000 } }
    { "_id" : ObjectId("5e1d66b240741afd9cfdee9d"), "user_id" : "B_id", "bonus" : { "type" : "b", "amount" : 3000 } }
    { "_id" : ObjectId("5e1d66d540741afd9cfdee9e"), "user_id" : "A_id", "bonus" : { "type" : "a", "amount" : 1000 } }
    { "_id" : ObjectId("5e1d66d540741afd9cfdee9e"), "user_id" : "A_id", "bonus" : { "type" : "b", "amount" : 2000 } }
    { "_id" : ObjectId("5e1d66d540741afd9cfdee9e"), "user_id" : "A_id", "bonus" : { "type" : "b", "amount" : 3000 } }

    3.查询数据:查询不同user_id下,数据元素type为b的amount之和与交易笔数

    MongoDB Enterprise > db.mytestcol.aggregate([{"$unwind":"$bonus"},{$match:{"bonus.type":"b"}},{$group:{"_id":"$user_id","amount":{$sum:"$bonus.amount"}}}])
    { "_id" : "B_id", "amount" : 5000 }
    { "_id" : "A_id", "amount" : 5000 }

    MongoDB Enterprise > db.mytestcol.aggregate([{"$unwind":"$bonus"},{$match:{"bonus.type":"b"}},{$group:{"_id":"$user_id","amount":{$sum:1}}}])
    { "_id" : "B_id", "amount" : 2 }
    { "_id" : "A_id", "amount" : 2 }

  • 相关阅读:
    match、match_phrase、term示例
    AVL树及java实现
    eclipse集成lombok注解不起作用
    红黑树原理详解
    为什么HashMap中链表长度超过8会转换成红黑树
    用deepin堆砌工作环境
    为什么黑客都不用鼠标?你听说过Linux吗?
    为什么二流程序员都喜欢黑php?
    看Linux 之父是如何定义 Linux?
    Linux 系统故障排查和修复技巧
  • 原文地址:https://www.cnblogs.com/xibuhaohao/p/12192161.html
Copyright © 2020-2023  润新知