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 }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM