$tsSecond (aggregation)

On this page本页内容

Definition定义

$tsSecond

New in version 5.1.在版本5.1中新增

Returns the seconds from a timestamp as a long.long形式返回timestamp中的秒数。

$tsSecond syntax:语法:

{ $tsSecond: <expression> }

The expression must resolve to a timestamp.表达式必须解析为timestamp

Tip提示
See also: 参阅:

Behavior行为

$tsSecond returns:返回:

  • Null if the input expression evaluates to null or refers to a field that is missing.如果输入表达式的计算结果为Null或引用缺少的字段,则为Null
  • An error if the input expression does not evaluate to a timestamp.如果输入表达式的计算结果不是timestamp,则为错误。

Examples示例

Obtain the Number of Seconds from a Timestamp Field从时间戳字段获取秒数

Create a stockSales collection that contains company stock financial market sales:创建包含公司股票金融市场销售额的stockSales集合:

db.stockSales.insertMany( [
   { _id: 0, symbol: "ABC", saleTimestamp: Timestamp(1622731060, 1) },
   { _id: 1, symbol: "ABC", saleTimestamp: Timestamp(1622731060, 2) },
   { _id: 2, symbol: "DEF", saleTimestamp: Timestamp(1714124193, 1) },
   { _id: 3, symbol: "DEF", saleTimestamp: Timestamp(1714124193, 2) },
   { _id: 4, symbol: "DEF", saleTimestamp: Timestamp(1714124193, 3) }
] )

In the timestamp constructor, the:timestamp构造函数中

  • First value is the number of seconds after the Unix epoch.第一个值是Unix历元后的秒数。
  • 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 $tsSecond in a $project pipeline stage to return the seconds from the stock sales saleTimestamp field:以下示例使用$project管道阶段中的$tsSecond从库存销售saleTimestamp字段返回秒数:

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

Example output:示例输出:

{
  saleTimestamp: Timestamp({ t: 1622731060, i: 1 }),
  saleSeconds: Long("1622731060")
},
{
  saleTimestamp: Timestamp({ t: 1622731060, i: 2 }),
  saleSeconds: Long("1622731060")
},
{
  saleTimestamp: Timestamp({ t: 1714124193, i: 1 }),
  saleSeconds: Long("1714124193")
},
{
  saleTimestamp: Timestamp({ t: 1714124193, i: 2 }),
  saleSeconds: Long("1714124193")
},
{
  saleTimestamp: Timestamp({ t: 1714124193, i: 3 }),
  saleSeconds: Long("1714124193")
}

Use $tsSecond in a Change Stream Cursor to Monitor Collection Changes在更改流游标中使用$tsSecond监视集合更改

The example in this section uses $tsSecond in a change stream cursor to monitor changes to a collection.本节中的示例使用更改流游标中的$tsSecond来监视对集合的更改。

Create a change stream cursor on a collection named cakeSales that you will see later in this section:在一个名为cakeSales的集合上创建一个变更流游标,您将在本节后面看到:

cakeSalesCursor = db.cakeSales.watch( [
   {
      $addFields: {
         clusterTimeSeconds: { $tsSecond: "$clusterTime" }
      }
   }
] )

In the example, the:在该示例中:

  • db.collection.watch() method creates a change stream cursor for the cakeSales collection and stores the cursor in cakeSalesCursor.方法为cakeSales集合创建更改流游标,并将游标存储在cakeSalesCursor中。
  • $addFields stage adds a field named clusterTimeSeconds to cakeSalesCursor.阶段将名为clusterTimeSeconds的字段添加到cakeSalesCursor

    • $clusterTime is the timestamp from the oplog entry for the cakeSales collection change. cakeSales集合更改的oplog条目的时间戳。See Command Response.请参阅命令响应
    • $tsSecond returns the seconds from $clusterTime, which is stored in clusterTimeSeconds.返回$clusterTime中的秒数,该秒数存储在clusterTimeSeconds中。

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集合更改,请使用cakeSalesCursorFor example, to obtain the next document from cakeSalesCursor, use the next() method:例如,要从cakeSalesCursor获取下一个文档,请使用next()方法:

cakeSalesCursor.next()

The following example output shows the insert details for the first document added to the cakeSales collection. 以下示例输出显示了添加到cakeSales集合的第一个文档的insert详细信息。The clusterTimeSeconds field contains the seconds from the clusterTime field.clusterTimeSeconds字段包含clusterTime字段中的秒数。

_id: {
  _data: '82613A4A51000000032B022C0100296E5A100495189B4131584C56AC8BA9D540799F23461E5F696400290004'
},
operationType: 'insert',
clusterTime: Timestamp({ t: 1631210065, i: 3 }),
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 },
clusterTimeSeconds: 1631210065
←  $tsIncrement (aggregation)$trim (aggregation) →