$minute (日期表达式)

适用对象: MongoDB vCore

运算符 $minute 从日期值中提取分钟部分,返回介于 0 和 59 之间的数字。 此运算符通常用于基于时间的分析和计划作。

语法

$minute 运算符的语法如下:

{
  $minute: <dateExpression>
}

参数

DESCRIPTION
dateExpression 解析为 Date、Timestamp 或 ObjectId 的表达式。 如果表达式解析为 null 或缺失, $minutenull返回 。

示例:

让我们了解数据集中示例 JSON 的 stores 用法。

{
  "_id": "905d1939-e03a-413e-a9c4-221f74055aac",
  "name": "Trey Research | Home Office Depot - Lake Freeda",
  "location": { "lat": -48.9752, "lon": -141.6816 },
  "staff": { "employeeCount": { "fullTime": 12, "partTime": 19 } },
  "sales": {
    "salesByCategory": [ { "categoryName": "Desk Lamps", "totalSales": 37978 } ],
    "revenue": 37978
  },
  "company": "Trey Research",
  "city": "Lake Freeda",
  "storeOpeningDate": ISODate("2024-09-26T22:55:25.779Z"),
  "lastUpdated": Timestamp({ "t": 1729983325, "i": 1 })
}

示例 1:从商店开张日期提取分钟数

本示例从商店开张日期中提取分钟部分,以分析打开时间模式。

db.stores.aggregate([
  { $match: {"_id": "905d1939-e03a-413e-a9c4-221f74055aac"} },
  {
    $project: {
      name: 1,
      storeOpeningDate: 1,
      openingMinute: {
        $minute: "$storeOpeningDate"
      }
    }
  }
])

查询从存储打开时间戳返回分钟部分(55)。

{
  "_id": "905d1939-e03a-413e-a9c4-221f74055aac",
  "name": "Trey Research | Home Office Depot - Lake Freeda",
  "storeOpeningDate": ISODate("2024-09-26T22:55:25.779Z"),
  "openingMinute": 55
}