MongoDB聚合运算符:$tsIncrement

MongoDB聚合运算符:$tsIncrement


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

语法

{ $tsIncrement: <expression> }

  • <expression>表达式必须能解析为时间戳。

使用

  • 如果输入表达式的值为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) }
] )

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

db.trigonometry.aggregate( [
   {
      $addFields : {
         "tanh_output" : { $tsIncrement : { $degreesToRadians : "$angle" } }
      }
   }
] )

在时间戳构造函数中:

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

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

db.stockSales.aggregate( [
   {
      $project:
      {
         _id: 0, saleTimestamp: 1, saleIncrement: { $tsIncrement: "$saleTimestamp" }
      }
   }
] )

在示例中,$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")
}

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

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

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

cakeSalesCursor = db.cakeSales.watch( [
   {
      $match: {
         $expr: {
            $eq: [
               { $mod: [ { $tsIncrement: "$clusterTime" } , 2 ] },
               0
            ]
         }
      }
   }
] )

在本例中:

  • 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 }
] )

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

cakeSalesCursor.next()

根据文档添加到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 }

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

相关推荐

  1. MongoDB聚合运算符:$tsIncrement

    2024-05-13 06:12:11       32 阅读
  2. MongoDB聚合运算符:$add

    2024-05-13 06:12:11       55 阅读
  3. MongoDB聚合运算符:$arrayToObject

    2024-05-13 06:12:11       53 阅读
  4. MongoDB聚合运算符;$dateToParts

    2024-05-13 06:12:11       53 阅读
  5. MongoDB聚合运算符:$dayOfWeek

    2024-05-13 06:12:11       55 阅读
  6. MongoDB聚合运算符:$dayOfMonth

    2024-05-13 06:12:11       52 阅读
  7. MongoDB聚合运算符;$dateToString

    2024-05-13 06:12:11       48 阅读
  8. MongoDB聚合运算符:$dayOfYear

    2024-05-13 06:12:11       48 阅读
  9. MongoDB聚合运算符:$denseRank

    2024-05-13 06:12:11       42 阅读
  10. MongoDB聚合运算符:$dateTrunc

    2024-05-13 06:12:11       46 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-13 06:12:11       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-13 06:12:11       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-13 06:12:11       87 阅读
  4. Python语言-面向对象

    2024-05-13 06:12:11       96 阅读

热门阅读

  1. Qt时钟的运用

    2024-05-13 06:12:11       33 阅读
  2. leetcode876-Middle of the Linked List

    2024-05-13 06:12:11       30 阅读
  3. CheckBox和CheckBoxList控件的使用

    2024-05-13 06:12:11       32 阅读
  4. leetcode203-Remove Linked List Elements

    2024-05-13 06:12:11       35 阅读
  5. Spark写Hbase如何提高Bulkload的速度

    2024-05-13 06:12:11       38 阅读
  6. SpringMVC

    SpringMVC

    2024-05-13 06:12:11      22 阅读
  7. 单元测试之JUnit5知识点总结及代码示例

    2024-05-13 06:12:11       24 阅读
  8. pytest(一)

    2024-05-13 06:12:11       26 阅读
  9. linux使用/etc/hosts.deny拒绝恶意ssh到本机

    2024-05-13 06:12:11       32 阅读