• MongoDB聚合运算符:$tsIncrement


    MongoDB聚合运算符:$tsIncrement


    $tsIncrement用来以 long形式返回时间戳的递增序数。当同一秒内发生多个事件时,递增序数唯一标识每个事件。

    语法

    { $tsIncrement: <expression> }
    
    
    • 1
    • 2
    • 表达式必须能解析为时间戳。

    使用

    • 如果输入表达式的值为null或引用的字段缺失,则$tsIncrement返回null
    • 如果输入表达式的值不是时间戳,则返回错误。

    举例

    时间戳字段获取递增序数

    使用下面的脚本创建stockSales集合,包含公司股票金融市场销售的情况:

    db.stockSales.insertMany( [
       { _id: 0, symbol: "MDB", saleTimestamp: Timestamp(1622731060, 1) },
       { _id: 1, symbol: "MDB", saleTimestamp: Timestamp(1622731060, 2) },
       { _id: 2, symbol: "MSFT", saleTimestamp: Timestamp(1714124193, 1) },
       { _id: 3, symbol: "MSFT", saleTimestamp: Timestamp(1714124193, 2) },
       { _id: 4, symbol: "MSFT", saleTimestamp: Timestamp(1714124193, 3) }
    ] )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    下面的聚合操作使用$tsIncrement表达式计算角度angle的双曲正切值,然后使用$addFields管道阶段将其添加到输入文档。

    db.trigonometry.aggregate( [
       {
          $addFields : {
             "tanh_output" : { $tsIncrement : { $degreesToRadians : "$angle" } }
          }
       }
    ] )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    在时间戳构造函数中:

    • 第一个值是 Unix 纪元之后的秒数。
    • 第二个值是递增序号。当同一秒内发生多个事件时,递增序号将唯一标识每个事件。

    下面的示例在 $project 阶段使用 $tsIncrement 从股票销售 salesTimestamp 字段返回递增序号:

    db.stockSales.aggregate( [
       {
          $project:
          {
             _id: 0, saleTimestamp: 1, saleIncrement: { $tsIncrement: "$saleTimestamp" }
          }
       }
    ] )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    在示例中,$project 只包括 saleTimestamp 和 saleIncrement 字段,如下输出所示:

    {
      saleTimestamp: Timestamp({ t: 1622731060, i: 1 }),
      saleIncrement: Long("1")
    },
    {
      saleTimestamp: Timestamp({ t: 1622731060, i: 2 }),
      saleIncrement: Long("2")
    },
    {
      saleTimestamp: Timestamp({ t: 1714124193, i: 1 }),
      saleIncrement: Long("1")
    },
    {
      saleTimestamp: Timestamp({ t: 1714124193, i: 2 }),
      saleIncrement: Long("2")
    },
    {
      saleTimestamp: Timestamp({ t: 1714124193, i: 3 }),
      saleIncrement: Long("3")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    在变化的数据流游标中使用 $tsIncrement 来监控集合变化

    下面的示例在变化数据流游标中使用了$tsIncrement,返回在同一秒钟内对集合所做的每一次更改。

    cakeSales集合上创建一个变化流游标:

    cakeSalesCursor = db.cakeSales.watch( [
       {
          $match: {
             $expr: {
                $eq: [
                   { $mod: [ { $tsIncrement: "$clusterTime" } , 2 ] },
                   0
                ]
             }
          }
       }
    ] )
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在本例中:

    • db.collection.watch()方法为cakeSales集合创建一个变化流游标,并将该游标存储在cakeSalesCursor中。
    • $match阶段使用$expr运算符对文档进行过滤。
    • $expr 运算符:
      • $mod 2应用于由$tsIncrement返回的$clusterTime变量的递增序号。
      • $clusterTime为修改cakeSales集合时来自oplog条目的时间戳。
      • 使用 $eq$mod返回值与0比较。

    创建一个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

    可以使用cakeSalesCursor来监视cakeSales集合的更改,例如,使用 next()方法获取cakeSalesCursor的下一个文档:

    cakeSalesCursor.next()
    
    • 1

    根据文档添加到cakeSales的秒数不同,cakeSalesCursor.next()的输出也不同,例如,文档添加时间可能超过一秒。

    下面的cakeSalesCursor.next()输出示例显示了添加到 cakeSales 集合的第一个文档的插入详细信息。注意,在 clusterTime 字段中,递增序号 i2

    _id: {
      _data: '82613A4F25000000022B022C0100296E5A100454C5BFAF538C47AB950614F43889BE00461E5F696400290004'
    },
    operationType: 'insert',
    clusterTime: Timestamp({ t: 1631211301, i: 2 }),
    fullDocument: {
      _id: 0,
      type: 'chocolate',
      orderDate: ISODate("2020-05-18T14:10:30.000Z"),
      state: 'CA',
      price: 13,
      quantity: 120
    },
    ns: { db: 'test', coll: 'cakeSales' },
    documentKey: { _id: 0 }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    再次运行 cakeSalesCursor.next(),返回clusterTime递增序号 i4cakeSales 文档,忽略 i3 的文档。

  • 相关阅读:
    rdma软件架构的理解。
    Ubuntu搭配POE交换机激活海康威视网络摄像头
    使用Docker部署Nacos集群
    IB中文A看人文教育
    Selenium基础:自动化你的网页交互
    源码分析:设备告警
    【云原生】Kubernetes网络管理实操
    计算机网络-网络,互联网
    Tomcat 源码解析一JNDI
    PhpSpreadsheet读写Excel文件
  • 原文地址:https://blog.csdn.net/superatom01/article/details/138756757