-
Notifications
You must be signed in to change notification settings - Fork 1.5k
DOCSP-35934: databases and collections #2816
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
Changes from 4 commits
7397ca6
9c313fd
c8263c0
cfcf100
2cbf27e
37c3940
d3e9691
95ed1df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,198 @@ | ||||||||||||||
.. _laravel-db-coll: | ||||||||||||||
|
||||||||||||||
========================= | ||||||||||||||
Databases and Collections | ||||||||||||||
========================= | ||||||||||||||
|
||||||||||||||
.. facet:: | ||||||||||||||
:name: genre | ||||||||||||||
:values: reference | ||||||||||||||
|
||||||||||||||
.. meta:: | ||||||||||||||
:keywords: php framework, odm | ||||||||||||||
|
||||||||||||||
.. contents:: On this page | ||||||||||||||
:local: | ||||||||||||||
:backlinks: none | ||||||||||||||
:depth: 2 | ||||||||||||||
:class: singlecol | ||||||||||||||
|
||||||||||||||
Overview | ||||||||||||||
-------- | ||||||||||||||
|
||||||||||||||
In this guide, you can learn how to use {+odm-short+} to access | ||||||||||||||
and manage MongoDB databases and collections. | ||||||||||||||
|
||||||||||||||
MongoDB organizes data in a hierarchical structure. A MongoDB | ||||||||||||||
deployment contains one or more **databases**, and each database | ||||||||||||||
contains one or more **collections**. In each collection, MongoDB stores | ||||||||||||||
data as **documents** that contain field-and-value pairs. In Laravel, | ||||||||||||||
documents are mapped to Eloquent models. | ||||||||||||||
|
||||||||||||||
To learn more about the document data format, | ||||||||||||||
see :manual:`Documents </core/document/>` in the Server manual. | ||||||||||||||
|
||||||||||||||
.. _laravel-access-db: | ||||||||||||||
|
||||||||||||||
Access a Database | ||||||||||||||
----------------- | ||||||||||||||
|
||||||||||||||
You can access a database by specifying a database connection in your | ||||||||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
application's ``config/database.php`` file. The ``connections`` property | ||||||||||||||
in this file stores all of your database connection information, such as | ||||||||||||||
your connection string, database name, and optionally, authentication | ||||||||||||||
details. After you specify a database connection, you can perform | ||||||||||||||
database-level operations and access collections that the database | ||||||||||||||
contains. | ||||||||||||||
|
||||||||||||||
If you set the ``database`` property in a connection to the name of a | ||||||||||||||
nonexistent database, Laravel still makes a valid connection. When you | ||||||||||||||
insert any data into a collection in the database, the server creates it | ||||||||||||||
automatically. | ||||||||||||||
|
||||||||||||||
The following example shows how to set a default database connection and | ||||||||||||||
create a database connection to the ``animals`` database in the | ||||||||||||||
``config/database.php`` file: | ||||||||||||||
|
||||||||||||||
.. code-block:: php | ||||||||||||||
:emphasize-lines: 8 | ||||||||||||||
|
||||||||||||||
'default' => env('DB_CONNECTION', 'mongodb'), | ||||||||||||||
|
||||||||||||||
'connections' => [ | ||||||||||||||
|
||||||||||||||
'mongodb' => [ | ||||||||||||||
'driver' => 'mongodb', | ||||||||||||||
'dsn' => env('DB_URI', '<connection string>'), | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The The application can contain a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe this is content that will be better suited for the Connection Guide There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is wrong. When a "dsn" is provided, the database name is read from the "dsn", and the "database" config is ignored. Your example should look like this, so we don't bother with env vars. 'mongodb' => [
'driver' => 'mongodb',
'dsn' => 'mongodb://localhost:27017/animals,
], There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Outside the scope of this PR, but is that something we should consider changing in some future release. The "auth database" component in the DSN should ideally never be specified since it was made obsolete by the |
||||||||||||||
'database' => 'animals', | ||||||||||||||
], ... | ||||||||||||||
] | ||||||||||||||
|
||||||||||||||
When you set a default database connection, {+odm-short+} uses that | ||||||||||||||
connection for operations, but you can specify multiple database connections | ||||||||||||||
in your ``config/database.php`` file. | ||||||||||||||
|
||||||||||||||
The following example shows how to specify multiple database connections | ||||||||||||||
(``mongodb`` and ``mongodb_alt``) to access the ``animals`` and | ||||||||||||||
``plants`` databases: | ||||||||||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
.. code-block:: php | ||||||||||||||
|
||||||||||||||
'connections' => [ | ||||||||||||||
|
||||||||||||||
'mongodb' => [ | ||||||||||||||
'driver' => 'mongodb', | ||||||||||||||
'dsn' => env('DB_URI', '<connection string>'), | ||||||||||||||
'database' => 'animals', | ||||||||||||||
], | ||||||||||||||
|
||||||||||||||
'mongodb_alt' => [ | ||||||||||||||
'driver' => 'mongodb', | ||||||||||||||
'dsn' => env('DB_URI', '<connection string>'), | ||||||||||||||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
'database' => 'plants', | ||||||||||||||
] | ||||||||||||||
|
||||||||||||||
], ... | ||||||||||||||
|
||||||||||||||
.. note:: | ||||||||||||||
|
||||||||||||||
The MongoDB PHP driver reuses the same connection when | ||||||||||||||
you create two clients with the same connection string. There is no | ||||||||||||||
overhead in using two connections for two distinct databases, so you | ||||||||||||||
do not need to optimize your connections. | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll note that if users expect to change the database name for separate connections, that would result in the driver not re-using the same connection since the connection string (i.e. This relates to my suggestion in https://github.com/mongodb/laravel-mongodb/pull/2816/files#r1559726362 to encourage users not to specify any "auth database" in the connection string, which the Laravel library just so happens to use as a "default" database. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the information! Since i modified the multiple connections example to specify the default db in the connection strings, should I remove this note then? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm asking @GromNaN above, but I very much dislike the example being presented here. If there's any way we can change this to avoid using the "auth database" URI component, I'd like to see it. I can confirm that this paragraph is no longer relevant given the current state of the PR, but the fact that it's now creating multiple database connections is very disagreeable to me. This doesn't feel like something we should be conveying so simply in docs that some users are bound to copy/paste as reference. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The correct configuration should be to extract the database into a dedicated config: 'connections' => [
'mongodb' => [
'driver' => 'mongodb',
'dsn' => 'mongodb://localhost:27017/',
'database' => 'animals',
],
'mongodb_alt' => [
'driver' => 'mongodb',
'dsn' => 'mongodb://localhost:27017/',
'database' => 'plants',
]
], ... |
||||||||||||||
|
||||||||||||||
If your application contains multiple | ||||||||||||||
database connections, you override the ``$connection`` | ||||||||||||||
property when creating a ``Model`` class to apply the model to an | ||||||||||||||
alternate database. | ||||||||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
The following example shows how to override the ``$connection`` property | ||||||||||||||
on the ``Flower`` model class to use the ``mongodb_alt`` connection: | ||||||||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
.. code-block:: php | ||||||||||||||
|
||||||||||||||
class Flower extends Model | ||||||||||||||
{ | ||||||||||||||
protected $connection = 'mongodb_alt'; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
.. _laravel-access-coll: | ||||||||||||||
|
||||||||||||||
Access a Collection | ||||||||||||||
------------------- | ||||||||||||||
|
||||||||||||||
When you create a ``Model`` class in {+odm-short+}, it is automatically | ||||||||||||||
stored in a collection with a name that is the plural of the model class | ||||||||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
name. For example, if you create a model class called ``Flower``, | ||||||||||||||
Laravel applies the model to the ``flowers`` collection in the database. | ||||||||||||||
|
||||||||||||||
.. tip:: | ||||||||||||||
|
||||||||||||||
You might be storing data in a collection with a different name than the | ||||||||||||||
model, or your collection might contain more than one type of data. To | ||||||||||||||
learn how to set a collection name when defining a model class, see the | ||||||||||||||
:ref:`laravel-model-customize-collection-name` section of the Eloquent | ||||||||||||||
Model Class guide. | ||||||||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
For many operations, such as CRUD operations and relationships, we | ||||||||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
recommend that you use the Eloquent ORM to access a collection | ||||||||||||||
implicitly. The following example specifies a find operation by using | ||||||||||||||
the ``Flower`` class, so Laravel retrieves results from the ``flowers`` | ||||||||||||||
collection: | ||||||||||||||
|
||||||||||||||
.. code-block:: php | ||||||||||||||
|
||||||||||||||
Flower::where('name', 'Water Lily')->get() | ||||||||||||||
|
||||||||||||||
For other collection operations, you can access a collection by using | ||||||||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
a facade to access the query builder class ``DB`` and calling the | ||||||||||||||
``collection()`` method. The following example shows the same query as | ||||||||||||||
in the preceding example constructed by using the ``DB::collection()`` | ||||||||||||||
method: | ||||||||||||||
|
||||||||||||||
.. code-block:: php | ||||||||||||||
|
||||||||||||||
DB::connection('mongodb') | ||||||||||||||
->collection('flowers') | ||||||||||||||
->where('name', 'Water Lily') | ||||||||||||||
->get() | ||||||||||||||
|
||||||||||||||
.. tip:: | ||||||||||||||
|
||||||||||||||
In MongoDB, each document in a collection has a field called ``_id`` which has a value | ||||||||||||||
that is a unique identifier. To learn more about customizing this field | ||||||||||||||
when you create a model to access a collection, see the :ref:`laravel-model-customize-primary-key` field of the Eloquent | ||||||||||||||
Model Class guide. | ||||||||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
List Collections | ||||||||||||||
---------------- | ||||||||||||||
|
||||||||||||||
To see the names of the collections in a database, call the | ||||||||||||||
``listCollections()`` method. | ||||||||||||||
|
||||||||||||||
The following example acceses a database connection, then | ||||||||||||||
calls the ``listCollections()`` method and outputs the collection names: | ||||||||||||||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
.. code-block:: php | ||||||||||||||
|
||||||||||||||
$collections = DB::connection('mongodb')->getMongoDB()->listCollections(); | ||||||||||||||
dump($collections); | ||||||||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
Drop a Collection | ||||||||||||||
----------------- | ||||||||||||||
|
||||||||||||||
We recommend performing a **migration** to handle dropping a collection. | ||||||||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
Deleting data from within a migration provides a controlled approach | ||||||||||||||
that ensures consistency, version control, and reversibility in your | ||||||||||||||
application. | ||||||||||||||
ccho-mongodb marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
|
||||||||||||||
To learn more about migrations, see the | ||||||||||||||
:ref:`laravel-eloquent-migrations` section in the Schema Builder guide. | ||||||||||||||
|
||||||||||||||
To drop a collection idiomatically, the Laravel ``Schema`` facade provides the | ||||||||||||||
``drop()`` and ``dropIfExists()`` methods. The following example | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @GromNaN: Since dropping a nonexistent collection is a NOP in PHP, is there any practical difference between these two methods? Does it make sense to talk about both of them given the driver behavior? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the doc should tell both methods have the same effect and they exist for compatibility with the SQL Eloquent. We could remove the collection existence check from this method? laravel-mongodb/src/Schema/Builder.php Lines 87 to 92 in 95c5b4f
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct. I see the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: "The Laravel Schema facade offers the drop() and dropIfExists() methods to drop a collection." Or, if you end up suggesting just one, maybe something like the following: "You can drop a collection by calling the "drop()" method of the Laravel Schema facade." |
||||||||||||||
demonstrates how to drop the ``flowers`` collection: | ||||||||||||||
|
||||||||||||||
.. code-block:: php | ||||||||||||||
|
||||||||||||||
Schema::dropIfExists('flowers'); |
Uh oh!
There was an error while loading. Please reload this page.