-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOCSP-38130: Time series collections #3274
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
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
afc751c
DOCSP-38130: Time sereies collections
norareidy 55b2470
apply phpcbf formatting
norareidy 79126ce
fix
norareidy 9c466da
Merge branch 'DOCSP-38130-time-series' of github.com:norareidy/larave…
norareidy 4a50208
build error
norareidy 4e7eff5
JT feedback
norareidy 38d929c
apply phpcbf formatting
norareidy bfb7511
fixes
norareidy 660e5f9
Merge branch 'DOCSP-38130-time-series' of github.com:norareidy/larave…
norareidy 0e239b4
apply phpcbf formatting
norareidy f037957
JT feedback 2
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
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,186 @@ | ||
.. _laravel-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 {+odm-short+} 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 | ||
|
||
.. _laravel-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 MongoDB Server 5.0 or later. | ||
|
||
You can create a time series collection to store time series data. | ||
To create a time series collection, create a migration class and | ||
add an ``up()`` function to specify the collection configuration. | ||
In the ``up()`` function, pass the new collection's name | ||
and the ``timeseries`` option to the ``Schema::create()`` method. | ||
|
||
.. tip:: | ||
|
||
To learn more about creating a migration class, see :ref:`laravel-eloquent-migrations` | ||
in the Schema Builder guide. | ||
|
||
When setting the ``timeseries`` 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'``. | ||
|
||
.. _laravel-time-series-create-example: | ||
|
||
Example | ||
~~~~~~~ | ||
|
||
This example migration class creates the ``precipitation`` time series collection | ||
with the following configuration: | ||
|
||
- ``timeField`` is set to ``'timestamp'`` | ||
- ``metaField`` is set to ``'location'`` | ||
- ``granularity`` is set to ``'minutes'`` | ||
|
||
.. literalinclude:: /includes/database-collection/time-series-migration.php | ||
:language: php | ||
:dedent: | ||
|
||
To verify that you successfully created the time series collection, call | ||
the ``Schema::hasCollection()`` method and pass the collection name as | ||
a parameter: | ||
|
||
.. code-block:: php | ||
|
||
$result = Schema::hasCollection('precipitation'); | ||
echo $result; | ||
|
||
The ``hasCollection()`` method returns a value of ``1`` if the | ||
collection exists. | ||
|
||
.. _laravel-time-series-insert: | ||
|
||
Insert Time Series Data | ||
----------------------- | ||
|
||
You can insert data into a time series collection by passing your documents to the ``insert()`` | ||
method and specifying the measurement, timestamp, and metadata in each inserted document. | ||
|
||
.. tip:: | ||
|
||
To learn more about inserting documents into a collection, see :ref:`laravel-fundamentals-insert-documents` | ||
in the Write Operations guide. | ||
|
||
Example | ||
~~~~~~~ | ||
|
||
This example inserts New York City precipitation data into the ``precipitation`` | ||
time series collection created in the :ref:`Create a Time Series Collection example | ||
<laravel-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 | ||
|
||
.. code-block:: php | ||
|
||
$data = [ | ||
[ | ||
'precipitation_mm' => 0.5, | ||
'location' => 'New York City', | ||
'timestamp' => new UTCDateTime(Carbon::create(2023, 9, 12, 0, 0, 0, 'CET')), | ||
], | ||
[ | ||
'precipitation_mm' => 2.8, | ||
'location' => 'New York City', | ||
'timestamp' => new UTCDateTime(Carbon::create(2023, 9, 17, 0, 0, 0, 'CET')), | ||
], | ||
]; | ||
|
||
$result = DB::table('precipitation') | ||
->insert($data); | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
.. note:: | ||
|
||
The preceding example uses the :ref:`Laravel query builder <laravel-query-builder>` | ||
to insert documents into the time series collection. Alternatively, | ||
you can create an Eloquent model that represents the collection and | ||
perform insert operations on your model. To learn more, see | ||
the :ref:`laravel-eloquent-model-class` guide. | ||
|
||
.. _laravel-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 <laravel-time-series-addtl-info>` section. | ||
|
||
.. _laravel-time-series-addtl-info: | ||
|
||
Additional Information | ||
---------------------- | ||
|
||
To learn more about the concepts mentioned in this guide, see the | ||
following MongoDB {+server-docs-name+} 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 querying data, see the :ref:`laravel-query-builder` guide. | ||
|
||
To learn more about performing aggregation operations, see the :ref:`laravel-aggregation-builder` | ||
guide. |
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
35 changes: 35 additions & 0 deletions
35
docs/includes/database-collection/time-series-migration.php
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,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
return new class extends Migration | ||
{ | ||
protected $connection = 'mongodb'; | ||
|
||
/** | ||
* Run the migrations. | ||
*/ | ||
public function up(): void | ||
{ | ||
$options = [ | ||
'timeseries' => [ | ||
'timeField' => 'timestamp', | ||
'metaField' => 'location', | ||
'granularity' => 'minutes', | ||
], | ||
]; | ||
|
||
Schema::create('precipitation', null, $options); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::drop('precipitation'); | ||
} | ||
}; |
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
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.