$tsIncrement (aggregation)
On this page本页内容
Definition定义
$tsIncrement
New in version 5.1. 5.1版新增。
Returns the incrementing ordinal from a timestamp as a 以long
.long
形式返回时间戳中的递增序号。
When multiple events happen within the same second, the incrementing ordinal uniquely identifies each event.当同一秒内发生多个事件时,递增的序数唯一地标识每个事件。
$tsIncrement
syntax:语法:
{ $tsIncrement: <expression> }
The expression must resolve to a timestamp.表达式必须解析为时间戳。
See also: 另请参阅:
Behavior行为
$tsIncrement
returns:退货:
如果输入表达式的计算结果为Null
if the input expression evaluates tonull
or refers to a field that is missing.Null
或引用了缺失的字段,则为Null
。An error if the input expression does not evaluate to a timestamp.如果输入表达式的计算结果不是时间戳,则会出现错误。
Examples实例
Obtain the Incrementing Ordinal from a Timestamp Field从时间戳字段中获取递增序号
Create a 创建一个包含公司股票金融市场销售的stockSales
collection that contains company stock financial market sales: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) }
] )
In the timestamp constructor, the:在timestamp构造函数中
First value is the number of seconds after the Unix epoch.第一个值是Unix epoch之后的秒数。
Second value is the incrementing ordinal. When multiple events happen within the same second, the incrementing ordinal uniquely identifies each event.第二个值是递增序数。当同一秒内发生多个事件时,递增的序数唯一地标识每个事件。
The following example uses 以下示例在$tsIncrement
in a $project
stage to return the incrementing ordinal from the stock sales saleTimestamp
field:$project
阶段中使用$tsIncrement
从股票销售saleTimestamp
字段返回递增序号:
db.stockSales.aggregate( [
{
$project:
{
_id: 0, saleTimestamp: 1, saleIncrement: { $tsIncrement: "$saleTimestamp" }
}
}
] )
In the example, 在该示例中,$project
only includes the saleTimestamp
and saleIncrement
fields as shown in the following output:$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")
}
Use $tsIncrement
in a Change Stream Cursor to Monitor Collection Changes在更改流游标中使用$tsIncrement
监视集合更改
$tsIncrement
in a Change Stream Cursor to Monitor Collection ChangesThe example in this section uses 本节中的示例在更改流游标中使用$tsIncrement
in a change stream cursor to return every other change made to a collection in the same second of time.$tsIncrement
来返回在同一秒内对集合所做的每一次其他更改。
Create a change stream cursor on a collection named 在名为cakeSales
that you will see later in this section:cakeSales
的集合上创建一个变更流游标,您将在本节稍后看到:
cakeSalesCursor = db.cakeSales.watch( [
{
$match: {
$expr: {
$eq: [
{ $mod: [ { $tsIncrement: "$clusterTime" } , 2 ] },
0
]
}
}
}
] )
In the example, the:在该示例中
db.collection.watch()
method creates a change stream cursor for the方法为cakeSales
collection and stores the cursor incakeSalesCursor
.cakeSales
集合创建一个变更流游标,并将该游标存储在cakeSalesCursor
中。$match
stage filters the documents to those returned by the$expr
operator.$match
阶段将文档筛选为$expr
运算符返回的文档。$expr
operator:运算符:Applies将$mod
2
to the$clusterTime
variable's incrementing ordinal returned by$tsIncrement
.$mod
2
应用于$tsIncrement
返回的$clusterTime
变量的递增序号。$clusterTime
is the timestamp from the oplog entry when the是修改cakeSales
collection is modified.cakeSales
集合时oplog条目的时间戳。See Command Response.请参见命令响应。Compares the returned value from使用$mod
to0
using$eq
.$eq
将$mod
的返回值与0
进行比较。
Create a 创建一个包含加利福尼亚州(cakeSales
collection that contains cake sales in the states of California (CA
) and Washington (WA
):CA
)和华盛顿州(WA
)蛋糕销售的cakeSales
系列:
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 }
] )
To monitor the 要监视cakeSales
collection changes, use cakeSalesCursor
. cakeSales
集合的更改,请使用cakeSalesCursor
。For example, to obtain the next document from 例如,要从cakeSalesCursor
, use the next()
method:cakeSalesCursor
获取下一个文档,请使用next()
方法:
cakeSalesCursor.next()
Depending on the second when the documents were added to cakeSales
, the output from cakeSalesCursor.next()
varies. For example, the document additions might span more than one second.cakeSalesCursor.next()
的输出会随着文档添加到cakeSales
的时间而变化。例如,添加文档的时间可能超过一秒钟。
The following 下面的cakeSalesCursor.next()
example output shows the insert
details for the first document added to the cakeSales
collection. Notice the incrementing ordinal i
is 2
in the clusterTime
field.cakeSalesCursor.next()
示例输出显示了添加到cakeSales
集合的第一个文档的插入详细信息。请注意clusterTime
字段中递增的序数i
为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 }
Running 再次运行cakeSalesCursor.next()
again returns the cakeSales
document for which the clusterTime
incrementing ordinal i
is 4
, omitting the document where i
is 3
.cakeSalesCursor.next()
将返回clusterTime
递增序号i
为4
的cakeSales
文档,省略i
为3
的文档。