-
Notifications
You must be signed in to change notification settings - Fork 34
DOCSP-41964: Time series collections #138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
norareidy
merged 7 commits into
mongodb:php-standardization
from
norareidy:DOCSP-41964-time-series
Sep 23, 2024
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
523c579
DOCSP-41964: Time series collections
norareidy 497cc4e
toc
norareidy 92d5a03
edits
norareidy 816a625
keywords
norareidy 619d9dd
SA feedback
norareidy 728baf9
JT feedback
norareidy c2aa893
Merge remote-tracking branch 'upstream/php-standardization' into DOCS…
norareidy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.. _php-databases-collections: | ||
|
||
========================= | ||
Databases and Collections | ||
========================= | ||
|
||
.. toctree:: | ||
:titlesonly: | ||
:maxdepth: 1 | ||
|
||
/databases-collections/time-series |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,213 @@ | ||
.. _php-time-series: | ||
|
||
======================= | ||
Time Series Collections | ||
======================= | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 1 | ||
:class: singlecol | ||
|
||
.. facet:: | ||
:name: genre | ||
:values: reference | ||
|
||
.. meta:: | ||
:keywords: chronological, data format, code example | ||
|
||
Overview | ||
-------- | ||
|
||
In this guide, you can learn how to use the {+php-library+} to create | ||
and interact with **time series collections**. These collections store | ||
time series data, which is composed of the following components: | ||
|
||
- Measured quantity | ||
- Timestamp for the measurement | ||
- Metadata that describes the measurement | ||
|
||
The following table describes sample situations for which you can store time | ||
series data: | ||
|
||
.. list-table:: | ||
:widths: 33, 33, 33 | ||
:header-rows: 1 | ||
:stub-columns: 1 | ||
|
||
* - Situation | ||
- Measured Quantity | ||
- Metadata | ||
|
||
* - Recording monthly sales by industry | ||
- Revenue in USD | ||
- Company, country | ||
|
||
* - Tracking weather changes | ||
- Precipitation level | ||
- Location, sensor type | ||
|
||
* - Recording fluctuations in housing prices | ||
- Monthly rent price | ||
- Location, currency | ||
|
||
.. _php-time-series-create: | ||
|
||
Create a Time Series Collection | ||
------------------------------- | ||
|
||
.. important:: Server Version for Time Series Collections | ||
|
||
To create and interact with time series collections, you must be | ||
connected to a deployment running {+mdb-server+} 5.0 or later. | ||
|
||
You can create a time series collection to store time series data. | ||
To create a time series collection, pass an options array to the | ||
``MongoDB\Database::createCollection()`` method that sets the | ||
``timeseries`` option. When setting this option, include the following fields: | ||
|
||
- ``timeField``: Specifies the field that stores a timestamp in each time series document. | ||
- ``metaField``: Specifies the field that stores metadata in each time series document. | ||
- ``granularity``: Specifies the approximate time between consecutive timestamps. The possible | ||
values are ``'seconds'``, ``'minutes'``, and ``'hours'``. | ||
|
||
.. _php-time-series-create-example: | ||
|
||
Example | ||
~~~~~~~ | ||
|
||
This example creates the ``sept2023`` time series collection in the | ||
``precipitation`` database with the following configuration: | ||
|
||
- ``timeField`` is set to ``'timestamp'`` | ||
- ``metaField`` is set to ``'location'`` | ||
- ``granularity`` is set to ``'minutes'`` | ||
|
||
.. literalinclude:: /includes/databases-collections/time-series.php | ||
:start-after: start-create-ts | ||
:end-before: end-create-ts | ||
:language: php | ||
:dedent: | ||
|
||
To verify that you successfully created the time series collection, call | ||
the ``MongoDB\Database::listCollections()`` method on the database and | ||
print the results: | ||
|
||
.. io-code-block:: | ||
:copyable: | ||
|
||
.. input:: /includes/databases-collections/time-series.php | ||
:start-after: start-list-colls | ||
:end-before: end-list-colls | ||
:language: php | ||
:dedent: | ||
|
||
.. output:: | ||
:language: console | ||
:visible: false | ||
|
||
MongoDB\Model\CollectionInfo Object | ||
( | ||
[name] => sept2023 | ||
[type] => timeseries | ||
[options] => Array | ||
( | ||
… | ||
) | ||
|
||
[info] => Array | ||
( | ||
… | ||
) | ||
) | ||
MongoDB\Model\CollectionInfo Object | ||
( | ||
[name] => system.buckets.sept2023 | ||
[type] => collection | ||
[options] => Array | ||
( | ||
… | ||
) | ||
|
||
[info] => Array | ||
( | ||
… | ||
) | ||
) | ||
|
||
.. note:: | ||
|
||
MongoDB stores system data associated with time series collections | ||
in the ``<database>.system.buckets`` namespace. For more information, | ||
see :manual:`database.system.buckets </reference/system-collections/#mongodb-data--database-.system.buckets>` | ||
in the {+mdb-server+} manual. | ||
|
||
.. _php-time-series-insert: | ||
|
||
Insert Time Series Data | ||
----------------------- | ||
|
||
You can insert data into a time series collection by using the ``MongoDB\Collection::insertOne()`` | ||
or ``MongoDB\Collection::insertMany()`` methods and specifying the measurement, | ||
timestamp, and metadata in each inserted document. | ||
|
||
.. tip:: | ||
|
||
To learn more about inserting documents into a collection, see the :ref:`php-write-insert` | ||
guide. | ||
|
||
Example | ||
~~~~~~~ | ||
|
||
This example inserts New York City precipitation data into the ``sept2023`` | ||
time series collection created in the :ref:`Create a Time Series Collection example | ||
<php-time-series-create-example>`. Each document contains the following fields: | ||
|
||
- ``precipitation_mm``, which stores precipitation measurements in millimeters | ||
- ``location``, which stores location metadata | ||
- ``timestamp``, which stores the time of the measurement collection | ||
|
||
.. literalinclude:: /includes/databases-collections/time-series.php | ||
:start-after: start-insert-ts | ||
:end-before: end-insert-ts | ||
:language: php | ||
:dedent: | ||
|
||
.. _php-time-series-query: | ||
|
||
Query Time Series Collections | ||
----------------------------- | ||
|
||
You can use the same syntax and conventions to query data stored in a time | ||
series collection as you use when performing read or aggregation operations on | ||
other collections. To find more information about these operations, see | ||
the :ref:`Additional Information <php-time-series-addtl-info>` section. | ||
|
||
.. _php-time-series-addtl-info: | ||
|
||
Additional Information | ||
---------------------- | ||
|
||
To learn more about the concepts mentioned in this guide, see the | ||
following Server manual entries: | ||
|
||
- :manual:`Time Series </core/timeseries-collections/>` | ||
- :manual:`Create and Query a Time Series Collection </core/timeseries/timeseries-procedures/>` | ||
- :manual:`Set Granularity for Time Series Data </core/timeseries/timeseries-granularity/>` | ||
|
||
To learn more about performing read operations, see :ref:`php-read`. | ||
|
||
To learn more about performing aggregation operations, see the :ref:`php-aggregation` | ||
guide. | ||
|
||
API Documentation | ||
~~~~~~~~~~~~~~~~~ | ||
|
||
To learn more about the methods mentioned in this guide, see the following | ||
API documentation: | ||
|
||
- :phpmethod:`MongoDB\Database::createCollection()` | ||
- :phpmethod:`MongoDB\Database::listCollections()` | ||
- :phpmethod:`MongoDB\Collection::insertOne()` | ||
- :phpmethod:`MongoDB\Collection::insertMany()` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
require 'vendor/autoload.php'; | ||
|
||
$uri = getenv('MONGODB_URI') ?: throw new RuntimeException('Set the MONGODB_URI variable to your Atlas URI that connects to the sample dataset'); | ||
$client = new MongoDB\Client($uri); | ||
|
||
// Creates a time series collection to store precipitation data | ||
// start-create-ts | ||
$db = $client->precipitation; | ||
|
||
$options = [ | ||
'timeseries' => [ | ||
'timeField' => 'timestamp', | ||
'metaField' => 'location', | ||
'granularity' => 'minutes' | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
] | ||
]; | ||
|
||
$collection = $db->createCollection('sept2023', $options); | ||
// end-create-ts | ||
|
||
// Lists the collections in the "precipitation" database | ||
// start-list-colls | ||
$cursor = $db->listCollections(); | ||
|
||
foreach ($cursor as $collectionInfo) { | ||
print_r($collectionInfo) . PHP_EOL; | ||
} | ||
// end-list-colls | ||
|
||
// Inserts precipitation time series data about New York City into the collection | ||
// start-insert-ts | ||
$collection = $db->sept2023; | ||
$result = $collection->insertMany( | ||
[ | ||
[ | ||
'precipitation_mm' => 0.5, | ||
'location' => 'New York City', | ||
'timestamp' => new MongoDB\BSON\UTCDateTime(1694829060000) | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
], | ||
[ | ||
'precipitation_mm' => 2.8, | ||
'location' => 'New York City', | ||
'timestamp' => new MongoDB\BSON\UTCDateTime(1695594780000) | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
] | ||
] | ||
); | ||
// end-insert-ts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.