MongoDB聚合操作符:$addToSet

$addToSet返回一个无重复元素的数组,元素值是对每个分组文档执行表达式的结果。数组元素顺序未指定。

$addToSet可以用于下列聚合阶段:

  • $bucket
  • $bucketAuto
  • $group
  • $setWindowFeilds

语法

{ $addToSet: }

用法

  • 如果表达式的值是个数组,$addToSet会把整个数组当成一个元素添加到返回的数组。
  • 如果表达式的值是一个文档,那么如果数组中的另一个文档与要添加的文档完全匹配,MongoDB 就会判定该文档为重复文档。具体来说,现有文档具有完全相同的字段和值,且顺序完全相同。

举例

$group阶段中使用

有一个sales集合包含以下文档:

{ "_id" : 1, "item" : "abc", "price" : 10, "quantity" : 2, "date" : ISODate("2014-01-01T08:00:00Z") }
{ "_id" : 2, "item" : "jkl", "price" : 20, "quantity" : 1, "date" : ISODate("2014-02-03T09:00:00Z") }
{ "_id" : 3, "item" : "xyz", "price" : 5, "quantity" : 5, "date" : ISODate("2014-02-03T09:05:00Z") }
{ "_id" : 4, "item" : "abc", "price" : 10, "quantity" : 10, "date" : ISODate("2014-02-15T08:00:00Z") }
{ "_id" : 5, "item" : "xyz", "price" : 5, "quantity" : 10, "date" : ISODate("2014-02-15T09:12:00Z") }

根据date字段按照日和年对文档进行分组,下面的操作使用$addToSet将分组内不重复的销售物品作为列表返回:

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

操作返回下面的结果:

{ "_id" : { "day" : 46, "year" : 2014 }, "itemsSold" : [ "xyz", "abc" ] }
{ "_id" : { "day" : 34, "year" : 2014 }, "itemsSold" : [ "xyz", "jkl" ] }
{ "_id" : { "day" : 1, "year" : 2014 }, "itemsSold" : [ "abc" ] }

$setWindowFields中使用

创建一个cakeSales集合,包含蛋糕在California (CA)Washington (WA)的销售记录:

db.cakeSales.insertMany( [
   { _id: 0, type: "chocolate", orderDate: new Date("2020-05-18T14:10:30Z"),
     state: "CA", price: 13, quantity: 120 },
   { _id: 1, type: "chocolate", orderDate: new Date("2021-03-20T11:30:05Z"),
     state: "WA", price: 14, quantity: 140 },
   { _id: 2, type: "vanilla", orderDate: new Date("2021-01-11T06:31:15Z"),
     state: "CA", price: 12, quantity: 145 },
   { _id: 3, type: "vanilla", orderDate: new Date("2020-02-08T13:13:23Z"),
     state: "WA", price: 13, quantity: 104 },
   { _id: 4, type: "strawberry", orderDate: new Date("2019-05-18T16:09:01Z"),
     state: "CA", price: 41, quantity: 162 },
   { _id: 5, type: "strawberry", orderDate: new Date("2019-01-08T06:12:03Z"),
     state: "WA", price: 43, quantity: 134 }
] )

下面在$setWindowFields集合中使用$addToSet,输出在每个州销售的不重复的蛋糕类型:

db.cakeSales.aggregate( [
   {
      $setWindowFields: {
         partitionBy: "$state",
         sortBy: { orderDate: 1 },
         output: {
            cakeTypesForState: {
               $addToSet: "$type",
               window: {
                  documents: [ "unbounded", "current" ]
               }
            }
         }
      }
   }
] )

本例中:

  • partitionBy: "$state"根据state对集合文档进行分区,分为CAWA两个分区。
  • sortBy: { orderDate: 1 }将每个分区中的文件按orderDate升序排序(1),最早的orderDate排在前面。
  • output使用运行在文档窗口的$addToSet把每个唯一的蛋糕类型type添加到cakeTypesForState数组字段。
    该窗口包含介于无限制下限unbounded和当前current文档之间的文档,这意味着$addToSet将返回一个数组,该数组包含分区开头和当前文档之间文档的唯一蛋糕类型type

本例在CAWAcakeTypesForState字段输出蛋糕类型type数组:

{ "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),
  "state" : "CA", "price" : 41, "quantity" : 162,
  "cakeTypesForState" : [ "strawberry" ] }
{ "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),
  "state" : "CA", "price" : 13, "quantity" : 120,
  "cakeTypesForState" : [ "strawberry", "chocolate" ] }
{ "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),
  "state" : "CA", "price" : 12, "quantity" : 145,
  "cakeTypesForState" : [ "strawberry", "vanilla", "chocolate" ] }
{ "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),
  "state" : "WA", "price" : 43, "quantity" : 134,
  "cakeTypesForState" : [ "strawberry" ] }
{ "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),
  "state" : "WA", "price" : 13, "quantity" : 104,
  "cakeTypesForState" : [ "vanilla", "strawberry" ] }
{ "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),
  "state" : "WA", "price" : 14, "quantity" : 140,
  "cakeTypesForState" : [ "vanilla", "chocolate", "strawberry" ] }

你可能感兴趣的:(mongodb,mongodb,数据库)