zoukankan      html  css  js  c++  java
  • 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 }

  • 相关阅读:
    disconf使用小结
    关于spring aop Advisor排序问题
    关于tomcat WEB-INF/lib下类加载顺序
    Netty5客户端源码解析
    巧用命令行工具 redis-cli
    redis学习总结
    聊聊Redis的持久化
    Git管理代码
    RabbitMQ连接池、生产者、消费者实例
    java处理节假日和工作时间的工具类
  • 原文地址:https://www.cnblogs.com/xibuhaohao/p/12192161.html
Copyright © 2011-2022 走看看