|
| 1 | +.. _laravel-db-coll: |
| 2 | + |
| 3 | +========================= |
| 4 | +Databases and Collections |
| 5 | +========================= |
| 6 | + |
| 7 | +.. facet:: |
| 8 | + :name: genre |
| 9 | + :values: reference |
| 10 | + |
| 11 | +.. meta:: |
| 12 | + :keywords: php framework, odm |
| 13 | + |
| 14 | +.. contents:: On this page |
| 15 | + :local: |
| 16 | + :backlinks: none |
| 17 | + :depth: 2 |
| 18 | + :class: singlecol |
| 19 | + |
| 20 | +Overview |
| 21 | +-------- |
| 22 | + |
| 23 | +In this guide, you can learn how to use {+odm-short+} to access |
| 24 | +and manage MongoDB databases and collections. |
| 25 | + |
| 26 | +MongoDB organizes data in a hierarchical structure. A MongoDB |
| 27 | +deployment contains one or more **databases**, and each database |
| 28 | +contains one or more **collections**. In each collection, MongoDB stores |
| 29 | +data as **documents** that contain field-and-value pairs. In |
| 30 | +{+odm-short+}, you can access documents through Eloquent models. |
| 31 | + |
| 32 | +To learn more about the document data format, |
| 33 | +see :manual:`Documents </core/document/>` in the Server manual. |
| 34 | + |
| 35 | +.. _laravel-access-db: |
| 36 | + |
| 37 | +Specify the Database in a Connection Configuration |
| 38 | +-------------------------------------------------- |
| 39 | + |
| 40 | +You can specify a database name that a connection uses in your |
| 41 | +application's ``config/database.php`` file. The ``connections`` property |
| 42 | +in this file stores all of your database connection information, such as |
| 43 | +your connection string, database name, and optionally, authentication |
| 44 | +details. After you specify a database connection, you can perform |
| 45 | +database-level operations and access collections that the database |
| 46 | +contains. |
| 47 | + |
| 48 | +If you set the database name in the ``database`` property to the name of a |
| 49 | +nonexistent database, Laravel still makes a valid connection. When you |
| 50 | +insert any data into a collection in the database, the server creates it |
| 51 | +automatically. |
| 52 | + |
| 53 | +The following example shows how to set a default database connection and |
| 54 | +create a database connection to the ``animals`` database in the |
| 55 | +``config/database.php`` file by setting the ``dsn`` and ``database`` properties: |
| 56 | + |
| 57 | +.. code-block:: php |
| 58 | + :emphasize-lines: 1,8 |
| 59 | + |
| 60 | + 'default' => 'mongodb', |
| 61 | + |
| 62 | + 'connections' => [ |
| 63 | + |
| 64 | + 'mongodb' => [ |
| 65 | + 'driver' => 'mongodb', |
| 66 | + 'dsn' => 'mongodb://localhost:27017/', |
| 67 | + 'database' => 'animals', |
| 68 | + ], ... |
| 69 | + ] |
| 70 | + |
| 71 | +When you set a default database connection, {+odm-short+} uses that |
| 72 | +connection for operations, but you can specify multiple database connections |
| 73 | +in your ``config/database.php`` file. |
| 74 | + |
| 75 | +The following example shows how to specify multiple database connections |
| 76 | +(``mongodb`` and ``mongodb_alt``) to access the ``animals`` and |
| 77 | +``plants`` databases: |
| 78 | + |
| 79 | +.. code-block:: php |
| 80 | + |
| 81 | + 'connections' => [ |
| 82 | + |
| 83 | + 'mongodb' => [ |
| 84 | + 'driver' => 'mongodb', |
| 85 | + 'dsn' => 'mongodb://localhost:27017/', |
| 86 | + 'database' => 'animals', |
| 87 | + ], |
| 88 | + |
| 89 | + 'mongodb_alt' => [ |
| 90 | + 'driver' => 'mongodb', |
| 91 | + 'dsn' => 'mongodb://localhost:27017/', |
| 92 | + 'database' => 'plants', |
| 93 | + ] |
| 94 | + |
| 95 | + ], ... |
| 96 | + |
| 97 | +.. note:: |
| 98 | + |
| 99 | + The MongoDB PHP driver reuses the same connection when |
| 100 | + you create two clients with the same connection string. There is no |
| 101 | + overhead in using two connections for two distinct databases, so you |
| 102 | + do not need to optimize your connections. |
| 103 | + |
| 104 | +If your application contains multiple database connections and you want |
| 105 | +to store your model in a database other than the default, override the |
| 106 | +``$connection`` property in your ``Model`` class. |
| 107 | + |
| 108 | +The following example shows how to override the ``$connection`` property |
| 109 | +on the ``Flower`` model class to use the ``mongodb_alt`` connection. |
| 110 | +This directs {+odm-short+} to store the model in the ``flowers`` |
| 111 | +collection of the ``plants`` database, instead of in the default database: |
| 112 | + |
| 113 | +.. code-block:: php |
| 114 | + |
| 115 | + class Flower extends Model |
| 116 | + { |
| 117 | + protected $connection = 'mongodb_alt'; |
| 118 | + } |
| 119 | + |
| 120 | +.. _laravel-access-coll: |
| 121 | + |
| 122 | +Access a Collection |
| 123 | +------------------- |
| 124 | + |
| 125 | +When you create model class that extends |
| 126 | +``MongoDB\Laravel\Eloquent\Model``, {+odm-short+} stores the model data |
| 127 | +in a collection with a name formatted as the snake case plural form of your |
| 128 | +model class name. |
| 129 | + |
| 130 | +For example, if you create a model class called ``Flower``, |
| 131 | +Laravel applies the model to the ``flowers`` collection in the database. |
| 132 | + |
| 133 | +.. tip:: |
| 134 | + |
| 135 | + To learn how to specify a different collection name in your model class, see the |
| 136 | + :ref:`laravel-model-customize-collection-name` section of the Eloquent |
| 137 | + Model Class guide. |
| 138 | + |
| 139 | +We generally recommend that you use the Eloquent ORM to access a collection |
| 140 | +for code readability and maintainability. The following |
| 141 | +example specifies a find operation by using the ``Flower`` class, so |
| 142 | +Laravel retrieves results from the ``flowers`` collection: |
| 143 | + |
| 144 | +.. code-block:: php |
| 145 | + |
| 146 | + Flower::where('name', 'Water Lily')->get() |
| 147 | + |
| 148 | +If you are unable to accomplish your operation by using an Eloquent |
| 149 | +model, you can access the query builder by calling the ``collection()`` |
| 150 | +method on the ``DB`` facade. The following example shows the same query |
| 151 | +as in the preceding example, but the query is constructed by using the |
| 152 | +``DB::collection()`` method: |
| 153 | + |
| 154 | +.. code-block:: php |
| 155 | + |
| 156 | + DB::connection('mongodb') |
| 157 | + ->collection('flowers') |
| 158 | + ->where('name', 'Water Lily') |
| 159 | + ->get() |
| 160 | + |
| 161 | +List Collections |
| 162 | +---------------- |
| 163 | + |
| 164 | +To see information about each of the collections in a database, call the |
| 165 | +``listCollections()`` method. |
| 166 | + |
| 167 | +The following example accesses a database connection, then |
| 168 | +calls the ``listCollections()`` method to retrieve information about the |
| 169 | +collections in the database: |
| 170 | + |
| 171 | +.. code-block:: php |
| 172 | + |
| 173 | + $collections = DB::connection('mongodb')->getMongoDB()->listCollections(); |
| 174 | + |
| 175 | +Create and Drop Collections |
| 176 | +--------------------------- |
| 177 | + |
| 178 | +To learn how to create and drop collections, see the |
| 179 | +:ref:`laravel-eloquent-migrations` section in the Schema Builder guide. |
0 commit comments