On this page本页内容
MongoDB stores times in UTC by default, and will convert any local time representations into this form. 默认情况下,MongoDB以UTC格式存储时间,并将任何本地时间表示形式转换为这种形式。Applications that must operate or report on some unmodified local time value may store the time zone alongside the UTC timestamp, and compute the original local time in their application logic.必须对某些未修改的本地时间值进行操作或报告的应用程序可能会将时区存储在UTC时间戳旁边,并在其应用程序逻辑中计算原始本地时间。
In the MongoDB shell, you can store both the current date and the current client’s offset from UTC.在MongoDB shell中,您可以存储当前日期和当前客户端与UTC的偏移量。
You can reconstruct the original local time by applying the saved offset:可以通过应用保存的偏移来重建原始本地时间:
A common method to organize time-series data is to group the data into buckets where each bucket represents a uniform unit of time such as a day or year. 组织时间序列数据的常用方法是将数据分组到多个存储桶中,其中每个存储桶表示一个一致性的时间单位,如一天或一年。Bucketing organizes specific groups of data to help:Bucketing组织特定的数据组以帮助:
Consider a collection that stores temperature data obtained from a sensor. 考虑存储从传感器获得的温度数据的集合。The sensor records the temperature every minute and stores the data in a collection called 传感器每分钟记录一次温度,并将数据存储在称为temperatures
:temperatures
的集合中:
This approach does not scale well in terms of data and index size. 这种方法在数据和索引大小方面不能很好地扩展。For example, if the application requires indexes on the 例如,如果应用程序需要sensor_id
and timestamp
fields, every incoming reading from the sensor would need to be indexed to improve performance.sensor_id
和timestamp
字段上的索引,则需要对来自传感器的每个传入读取进行索引以提高性能。
You can leverage the document model to bucket the data into documents that hold the measurements for a particular timespan. 您可以利用文档模型将数据存储到保存特定时间跨度的度量的文档中。Consider the following updated schema which buckets the readings taken every minute into hour-long groups:考虑下面的更新模式,它将每分钟读取的读数转换成小时长组:
This updated schema improves scalability and mirrors how the application actually uses the data. 此更新的模式提高了可伸缩性,并反映了应用程序实际使用数据的方式。A user likely wouldn’t query for a specific temperature reading. 用户可能不会查询特定的温度读数。Instead, a user would likely query for temperature behavior over the course of an hour or day. 相反,用户可能会查询一小时或一天内的温度行为。The Bucket pattern helps facilitate those queries by grouping the data into uniform time periods.Bucket模式通过将数据分组到一致性的时间段来帮助简化这些查询。
The example document contains two computed fields: 示例文档包含两个计算字段:事务计数和总和温度。transaction_count
and sum_temperature
. If the application frequently needs to retrieve the sum of temperatures for a given hour, computing a running total of the sum can help save application resources. 如果应用程序经常需要检索给定小时的温度总和,则计算该总和的运行总数可以帮助节省应用程序资源。This Computed Pattern approach eliminates the need to calculate the sum each time the data is requested.这种计算模式方法消除了每次请求数据时计算总和的需要。
The pre-aggregated 预聚合的sum_temperature
and transaction_count
values enable further computations such as the average temperature (sum_temperature
/ transaction_count
) for a particular bucket. sum_temperature
和transaction_count
值支持进一步计算,例如特定存储桶的平均温度(总和温度/事务计数)。It is much more likely that users will query the application for the average temperature between 2:00 and 3:00 PM rather than querying for the specific temperature at 2:03 PM. Bucketing and pre-computing certain values allows the application to more readily provide that information.用户很可能会在下午2:00到3:00之间查询应用程序的平均温度,而不是查询下午2:03的具体温度。Bucketing和预先计算某些值允许应用程序更容易地提供该信息。
In addition to time-series data, the Bucket pattern is useful for Internet of Things projects where you have multiple datasets coming from many different sources. 除了时间序列数据外,Bucket模式对于物联网项目也很有用,因为在物联网项目中,有多个来自不同来源的数据集。It can be helpful to bucket that data into groups (e.g. based on device type or system) to more easily retrieve and parse the data.将数据分组(例如,基于设备类型或系统)有助于更轻松地检索和解析数据。
The Bucket pattern is also commonly used in financial applications to group transactions by type, date, or customer.Bucket模式也常用于金融应用程序中,用于按类型、日期或客户对交易进行分组。