• MongoDB聚合运算符:$documentNumber


    $documentNumber聚合运算符返回$setWindowFields聚合阶段文档在分区中的位置。从版本5.0开始支持。

    语法

    { $documentNumber: { } }
    
    • 1

    $documentNumber聚合运算符没有参数。

    用法

    • $setWindowFields阶段的sortBy字段决定了文档的编号。
    • $documentNumber返回文档在分区中位置的唯一数字,即便是sortBy字段的值重复也会返回唯一值。
    • $documentNumber只能应用于$setWindowFields阶段
    • $documentNumber包括了sortBy字段为null或缺失的文档。
    • $documentNumber$rank$denseRank返回文档的位置都基于sortBy字段的值。
    • $documentNumber$rank$denseRank的区别在于对sortBy字段重复值的处理规则不同。
      • $rank$denseRank对于排序字段相同值的文档会返回相同的位置(称为排名)
      • $documentNumber返回文档的唯一位置(称为文档编号)

    举例

    每个州的文档编号

    使用下面的脚本创建cakeSales集合,包含了蛋糕在加利福尼亚州(CA)和华盛顿州(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 }
    ] )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    下面的聚合脚本在$setWindowFields阶段使用$documentNumber输出每个州蛋糕销售文档的编号:

    db.cakeSales.aggregate( [
       {
          $setWindowFields: {
             partitionBy: "$state",
             sortBy: { quantity: -1 },
             output: {
                documentNumberForState: {
                   $documentNumber: {}
                }
             }
          }
       }
    ] )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在本例中:

    • partitionBy: "$state"对集合文档按照state字段进行分区,共有CAWA两个州。
    • sortBy: { quantity: -1 }根据quantity对文档进行逆序排序,quantity最大的在最前面
    • output将文档编号的值赋予字段documentNumberForState,并且在所在州中唯一

    结果如下:

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

    重复值、空值和缺失值的文档编号

    创建一个cakeSalesWithDuplicates集合:

    db.cakeSalesWithDuplicates.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 },
       { _id: 6, type: "strawberry", orderDate: new Date("2020-01-08T06:12:03Z"),
         state: "WA", price: 41, quantity: 134 },
       { _id: 7, type: "strawberry", orderDate: new Date("2020-01-01T06:12:03Z"),
         state: "WA", price: 34, quantity: 134 },
       { _id: 8, type: "strawberry", orderDate: new Date("2020-01-02T06:12:03Z"),
         state: "WA", price: 40, quantity: 134 },
       { _id: 9, type: "strawberry", orderDate: new Date("2020-05-11T16:09:01Z"),
         state: "CA", price: 39, quantity: 162 },
       { _id: 10, type: "strawberry", orderDate: new Date("2020-05-11T16:09:01Z"),
         state: "CA", price: 39, quantity: null },
       { _id: 11, type: "strawberry", orderDate: new Date("2020-05-11T16:09:01Z"),
         state: "CA", price: 39 }
    ] )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在集合中:

    • 蛋糕销售的地点有加利福尼亚州(CA)和华盛顿州(WA)
    • 文档6到8与文档5的quantitystate相同
    • 文档9与文档4的quantitystate相同
    • 文档10的quantitynull
    • 文档11的quantity字段缺失

    下面的例子在$setWindowFields阶段使用$documentNumber输出文档在每个州的编号:

    db.cakeSalesWithDuplicates.aggregate( [
       {
          $setWindowFields: {
             partitionBy: "$state",
             sortBy: { quantity: -1 },
             output: {
                documentNumberForState: {
                   $documentNumber: {}
                }
             }
          }
       }
    ] )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    在本例中:

    • partitionBy: "$state"对集合文档按照state字段进行分区,共有CAWA两个州。
    • sortBy: { quantity: -1 }根据quantity对文档进行逆序排序,quantity最大的在最前面
    • output将文档编号的值赋予字段documentNumberForState,并且在所在州中唯一,无论文档的quantity字段的值为null还是缺失。

    结果如下:

    { "_id" : 4, "type" : "strawberry", "orderDate" : ISODate("2019-05-18T16:09:01Z"),
      "state" : "CA", "price" : 41, "quantity" : 162, "documentNumberForState" : 1 }
    { "_id" : 9, "type" : "strawberry", "orderDate" : ISODate("2020-05-11T16:09:01Z"),
      "state" : "CA", "price" : 39, "quantity" : 162, "documentNumberForState" : 2 }
    { "_id" : 2, "type" : "vanilla", "orderDate" : ISODate("2021-01-11T06:31:15Z"),
      "state" : "CA", "price" : 12, "quantity" : 145, "documentNumberForState" : 3 }
    { "_id" : 0, "type" : "chocolate", "orderDate" : ISODate("2020-05-18T14:10:30Z"),
      "state" : "CA", "price" : 13, "quantity" : 120, "documentNumberForState" : 4 }
    { "_id" : 10, "type" : "strawberry", "orderDate" : ISODate("2020-05-11T16:09:01Z"),
      "state" : "CA", "price" : 39, "quantity" : null, "documentNumberForState" : 5 }
    { "_id" : 11, "type" : "strawberry", "orderDate" : ISODate("2020-05-11T16:09:01Z"),
      "state" : "CA", "price" : 39, "documentNumberForState" : 6 }
    { "_id" : 1, "type" : "chocolate", "orderDate" : ISODate("2021-03-20T11:30:05Z"),
      "state" : "WA", "price" : 14, "quantity" : 140, "documentNumberForState" : 1 }
    { "_id" : 5, "type" : "strawberry", "orderDate" : ISODate("2019-01-08T06:12:03Z"),
      "state" : "WA", "price" : 43, "quantity" : 134, "documentNumberForState" : 2 }
    { "_id" : 6, "type" : "strawberry", "orderDate" : ISODate("2020-01-08T06:12:03Z"),
      "state" : "WA", "price" : 41, "quantity" : 134, "documentNumberForState" : 3 }
    { "_id" : 7, "type" : "strawberry", "orderDate" : ISODate("2020-01-01T06:12:03Z"),
      "state" : "WA", "price" : 34, "quantity" : 134, "documentNumberForState" : 4 }
    { "_id" : 8, "type" : "strawberry", "orderDate" : ISODate("2020-01-02T06:12:03Z"),
      "state" : "WA", "price" : 40, "quantity" : 134, "documentNumberForState" : 5 }
    { "_id" : 3, "type" : "vanilla", "orderDate" : ISODate("2020-02-08T13:13:23Z"),
      "state" : "WA", "price" : 13, "quantity" : 104, "documentNumberForState" : 6 }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
  • 相关阅读:
    Cesium快速上手7-3dtiles加载
    中文编程工具免费版下载,中文开发语言工具免费版下载
    机器学习文献|基于循环细胞因子特征,通过机器学习算法预测NSCLC免疫治疗结局
    使用PyTorch实现简单的AlphaZero的算法(2):理解和实现蒙特卡洛树搜索
    Spring源码之invokeBeanFactoryPostProcessors扫描、BeanDefinition生成
    模拟输入信号保护方法,确保数据准确性和系统稳定性
    Android 12/12L 全面升级、微信和“吃鸡”都在用 Flutter,2021 Google 开发者大会你看了吗?
    【Git企业开发】第七节.多人协作开发
    Python安装
    Python tkinter -- 第18章 画布控件之图像(image)
  • 原文地址:https://blog.csdn.net/superatom01/article/details/136657482