Skip to content

mongodb

aggregate

typescript
    .aggregate([
      {
        $group: {
          _id: "$_from",
          project: { $addToSet: "$ProjectName" },
        },
      },
      { $unwind: "$project" },
      {
        $project: {
          _id: 0,
          item: { $concat: ["$project", "-", "$_id"] },
        },
      },
    ])

管道

1615527207277-e3a45d14-d4d0-4b2c-b109-53c4552a25a2.png

$match,$unwind,$project

https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/

cheatsheet

http://haydnjm.com/mongo-cheatsheet

中文资料

https://ithelp.ithome.com.tw/articles/10185952

操作符號功用
$project選擇集合中要的欄位,並可進行修改。
$match篩選操作,可以減少不需要的資料。
$group可以欄位進行分組。
$unwind拆開,可以將陣列欄位拆開成多個document
$sort可針對欄位進行排序 。
$limit可針對回傳結果進行數量限制。
$skip略過前n
筆資料,在開始回傳 。

Pipeline Stages

https://docs.mongodb.com/manual/reference/operator/aggregation-pipeline/

1615539922636-23aa9b72-30ad-4a2c-9d16-07c30dbf5c40.png

Pipeline Operators

typescript
db.sales.aggregate(
   [
     {
       $group:
         {
           _id: { day: { $dayOfYear: "$date"}, year: { $year: "$date" } },
           itemsSold: { $push:  { item: "$item", quantity: "$quantity" } }
         }
     }
   ]
)

连表查询

https://blog.csdn.net/u011113654/article/details/80353013

数组扁平化

https://www.shuzhiduo.com/A/ZOJPVxxKdv/

嵌套数组删除成员[数组更新操作符]

https://docs.mongodb.com/manual/reference/operator/update/pull/

1615539585369-6dfed462-a7e7-4ff6-b586-64b84bc4b512.png

aws 的 documentDB 很多不支持

  • $$root
  • $first
  • mapReduce
  • $reduce

参考 https://docs.aws.amazon.com/zh_cn/documentdb/latest/developerguide/mongo-apis.html

更新: 2021-03-30 14:06:25
原文: https://www.yuque.com/u3641/dxlfpu/eg29uc