定义
5.1 版本中的新功能。
以 long 的形式返回时间戳中的递增序数
当多个事件在同一秒内发生时,递增序数词唯一标识每个事件。
$tsIncrement 事务语法:
{ $tsIncrement: <expression> }
行为
$tsIncrement 返回:
示例
从时间戳字段中获取递增序数
stockSales创建包含公司股票金融市场销售额的collection集合:
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 $project从股票销售saleTimestamp 字段返回递增序数:
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中。$expr操作符:
创建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 } ] )
要监控 cakeSales 集合变化,请使用 cakeSalesCursor。例如,要从 cakeSalesCursor 获取下一个文档,请使用 next() 方法:
cakeSalesCursor.next()
根据将文档添加到cakeSales的秒数, cakeSalesCursor.next()的输出会有所不同。 例如,文档添加的时间可能超过一秒。
以下cakeSalesCursor.next()示例输出显示了添加到cakeSales集合中的第一个文档的insert详细信息。 请注意,递增序数词i在clusterTime字段中是2 。
_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递增序数词i为4的cakeSales文档,而忽略i为3的文档。