|
| 1 | +============== |
| 2 | +BSON Documents |
| 3 | +============== |
| 4 | + |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +MongoDB is a document-based database system, and as a result, all |
| 8 | +records, or data, in MongoDB are documents. Documents are the default |
| 9 | +representation of most user accessible data structures in the |
| 10 | +database. Documents provide structure for data in the following |
| 11 | +MongoDB contexts: |
| 12 | + |
| 13 | +- the :ref:`records <documents-records>` stored in :term:`collections |
| 14 | + <collection>` |
| 15 | + |
| 16 | +- the :ref:`query selectors <documents-query-selectors>` that determine |
| 17 | + which records to select for read, update, and delete operations |
| 18 | + |
| 19 | +- the :ref:`update actions <documents-update-actions>` that specify |
| 20 | + the particular field updates to perform during an update operation |
| 21 | + |
| 22 | +- the specification of :ref:`indexes <documents-index>` for |
| 23 | + collection. |
| 24 | + |
| 25 | +- arguments to several MongoDB methods and operators, including: |
| 26 | + |
| 27 | + - :ref:`sort order <documents-sort-order>` for the :method:`sort() |
| 28 | + <cursor.sort()>` method. |
| 29 | + |
| 30 | + - :ref:`index specification <documents-index>` for the |
| 31 | + :method:`hint() <cursor.hint()>` method. |
| 32 | + |
| 33 | +- the output of a number of MongoDB commands and operations, including: |
| 34 | + |
| 35 | + - the :doc:`output </reference/collection-statistics>` |
| 36 | + of :dbcommand:`collStats` command, and |
| 37 | + |
| 38 | + - the :doc:`output </reference/server-status>` of the |
| 39 | + :dbcommand:`serverStatus` command. |
| 40 | + |
| 41 | +Structure |
| 42 | +--------- |
| 43 | + |
| 44 | +The document structure in MongoDB are :term:`BSON` objects with |
| 45 | +support for the full range of :term:`BSON types`; however, BSON |
| 46 | +documents are conceptually, similar to :term:`JSON` objects, and have |
| 47 | +the following structure: |
| 48 | + |
| 49 | +.. code-block:: javascript |
| 50 | + |
| 51 | + { |
| 52 | + field1: value1, |
| 53 | + field2: value2, |
| 54 | + field3: value3, |
| 55 | + ... |
| 56 | + fieldN: valueN |
| 57 | + } |
| 58 | + |
| 59 | +Having support for the full range of :term:`BSON types`, MongoDB |
| 60 | +documents may contain field and value pairs where the value can be |
| 61 | +another document, an array, an array of documents as well |
| 62 | +as the basic types such as ``Double``, ``String``, or ``Date``. |
| 63 | + |
| 64 | +Consider the following document that contains values of varying |
| 65 | +types: |
| 66 | + |
| 67 | +.. code-block:: javascript |
| 68 | + |
| 69 | + { |
| 70 | + _id: ObjectId("5099803df3f4948bd2f98391"), |
| 71 | + name: { first: "Alan", last: "Turing" }, |
| 72 | + birth: new Date('Jun 23, 1912'), |
| 73 | + death: new Date('Jun 07, 1954'), |
| 74 | + contribs: [ "Turing machine", "Turing test", "Turingery" ], |
| 75 | + views : NumberLong(1250000), |
| 76 | + update : Timestamp(1352237167000, 1) |
| 77 | + } |
| 78 | + |
| 79 | +The document contains the following fields: |
| 80 | + |
| 81 | +- ``_id`` that holds an *ObjectId*. |
| 82 | + |
| 83 | +- ``name`` that holds a *sub-document* that contains the fields |
| 84 | + ``first`` and ``last``. |
| 85 | + |
| 86 | +- ``birth`` and ``death``, which both have *Date* types. |
| 87 | + |
| 88 | +- ``contribs``, which holds an *array of strings*. |
| 89 | + |
| 90 | +- ``views`` that holds a value of *NumberLong* type. |
| 91 | + |
| 92 | +- ``update`` that holds a value of *Timestamp* type. |
| 93 | + |
| 94 | +Types |
| 95 | +----- |
| 96 | + |
| 97 | +.. _documents-records: |
| 98 | + |
| 99 | +Record Documents |
| 100 | +~~~~~~~~~~~~~~~~ |
| 101 | + |
| 102 | +Most documents in MongoDB are records in :term:`collections` which |
| 103 | +store data from users' applications. |
| 104 | + |
| 105 | +These documents have the following limitations: |
| 106 | + |
| 107 | +- .. include:: /includes/fact-document-max-size.rst |
| 108 | + |
| 109 | +- .. include:: /includes/fact-document-field-name-restrictions.rst |
| 110 | + |
| 111 | +.. include:: /includes/note-insert-id-field.rst |
| 112 | + |
| 113 | +The following document specifies a record in a collection: |
| 114 | + |
| 115 | + .. code-block:: javascript |
| 116 | + |
| 117 | + { |
| 118 | + _id: 1, |
| 119 | + name: { first: 'John', last: 'Backus' }, |
| 120 | + birth: new Date('Dec 03, 1924'), |
| 121 | + death: new Date('Mar 17, 2007'), |
| 122 | + contribs: [ 'Fortran', 'ALGOL', 'Backus-Naur Form', 'FP' ], |
| 123 | + awards: [ |
| 124 | + { award: 'National Medal of Science', |
| 125 | + year: '1975', |
| 126 | + by: 'National Science Foundation' }, |
| 127 | + { award: 'Turing Award', |
| 128 | + year: '1977', |
| 129 | + by: 'ACM' } |
| 130 | + ] |
| 131 | + } |
| 132 | + |
| 133 | +The document contains the following fields: |
| 134 | + |
| 135 | +- ``_id``, which must hold a unique value. |
| 136 | + |
| 137 | +- ``name`` that holds another *document*. This sub-document contains |
| 138 | + the fields ``first`` and ``last``, which both hold *strings*. |
| 139 | + |
| 140 | +- ``birth`` and ``death`` that both have *date* types. |
| 141 | + |
| 142 | +- ``contribs`` that holds an *array of strings*. |
| 143 | + |
| 144 | +- ``awards`` that holds an *array of documents*. |
| 145 | + |
| 146 | +.. _documents-query-selectors: |
| 147 | + |
| 148 | +Query Specification Documents |
| 149 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 150 | + |
| 151 | +Query selector documents specify the conditions that determine which |
| 152 | +records to select for read, update, and delete operations. You can use |
| 153 | +field and value expressions to specify the equality condition and |
| 154 | +:doc:`query operator </reference/operators>` expressions to specify |
| 155 | +additional conditions. Refer to :doc:`read </applications/read>`, |
| 156 | +:doc:`update </applications/update>`, and :doc:`delete |
| 157 | +</applications/delete>` pages for more examples. |
| 158 | + |
| 159 | +Consider the following examples of query selector documents: |
| 160 | + |
| 161 | +- The following document specifies the query criteria where ``_id`` is |
| 162 | + equal to ``1``: |
| 163 | + |
| 164 | + .. code-block:: javascript |
| 165 | + |
| 166 | + { _id: 1 } |
| 167 | + |
| 168 | +- The following document specifies the query criteria where ``_id`` is |
| 169 | + greater than ``3``: |
| 170 | + |
| 171 | + .. code-block:: javascript |
| 172 | + |
| 173 | + { _id: { $gt: 3 } } |
| 174 | + |
| 175 | +- The following document specifies the compound query criteria where |
| 176 | + ``_id`` is equal to ``1`` **and** the ``name`` field equals the |
| 177 | + document ``{ first: 'John', last: 'Backus' }``: |
| 178 | + |
| 179 | + .. code-block:: javascript |
| 180 | + |
| 181 | + { _id: 1, name: { first: 'John', last: 'Backus' } } |
| 182 | + |
| 183 | +- The following document specifies the compound query criteria where |
| 184 | + ``_id`` is equal to ``1`` **or** the ``name`` field equals the |
| 185 | + document ``{ first: 'John', last: 'Backus' }``: |
| 186 | + |
| 187 | + .. code-block:: javascript |
| 188 | + |
| 189 | + { $or: [ { _id: 1 }, { name: { first: 'John', last: 'Backus' } } ] } |
| 190 | + |
| 191 | +When passed as an argument to methods such as the :method:`find() |
| 192 | +<db.collection.find()>` method, the :method:`remove() |
| 193 | +<db.collection.remove()>` method, or the :method:`update() |
| 194 | +<db.collection.update()>` method, the query document selects documents |
| 195 | +for MongoDB to return, remove, or update, as in the following: |
| 196 | + |
| 197 | +.. code-block:: javascript |
| 198 | + |
| 199 | + db.csbios.find( { _id: 1 } ) |
| 200 | + db.csbios.remove( { _id: { $gt: 3 } } ) |
| 201 | + db.csbios.update( { _id: 1, name: { first: 'John', last: 'Backus' } }, |
| 202 | + ... ) |
| 203 | + |
| 204 | +.. _documents-update-actions: |
| 205 | + |
| 206 | +Update Specification Documents |
| 207 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 208 | + |
| 209 | +The update action documents specify the data modifications to perform |
| 210 | +during an :method:`update() <db.collection.update()>` operation to |
| 211 | +modify existing records in a collection. You can use :ref:`update |
| 212 | +operators <update-operators>` to specify the exact actions to perform |
| 213 | +on the document fields. See the :ref:`update operators |
| 214 | +<update-operators>` page for the available update operators and syntax. |
| 215 | + |
| 216 | +Consider the update specification document example: |
| 217 | + |
| 218 | +.. code-block:: javascript |
| 219 | + |
| 220 | + { $set: { 'name.middle': 'Warner' }, |
| 221 | + $push: { awards: { award: 'IBM Fellow', |
| 222 | + year: '1963', |
| 223 | + by: 'IBM' } |
| 224 | + |
| 225 | +When passed as an argument to the :method:`update() |
| 226 | +<db.collection.update()>` method, the update actions document: |
| 227 | + |
| 228 | +- Modifies the field ``name`` whose value is another document. |
| 229 | + Specifically, the :operator:`$set` operator updates the ``middle`` |
| 230 | + field in the ``name`` subdocument. |
| 231 | + |
| 232 | +- Adds an element to the field ``awards`` whose value is an array. |
| 233 | + Specifically, the :operator:`$push` operator adds another document as |
| 234 | + element to the field ``awards``. |
| 235 | + |
| 236 | +.. code-block:: javascript |
| 237 | + |
| 238 | + db.csbios.update( { _id: 1 }, |
| 239 | + { $set: { 'name.middle': 'Warner' }, |
| 240 | + $push: { awards: { award: 'IBM Fellow', |
| 241 | + year: '1963', |
| 242 | + by: 'IBM' } } } |
| 243 | + ) |
| 244 | + |
| 245 | +.. _documents-index: |
| 246 | +.. _document-index-specification: |
| 247 | + |
| 248 | +Index Specification Documents |
| 249 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 250 | + |
| 251 | +Indexes optimize a number of key :doc:`read </core/read-operations>` |
| 252 | +and :doc:`write </core/write-operations>` operations. Index |
| 253 | +specification documents describe the fields to index on during the |
| 254 | +:ref:`index creation </reference/method/db.collection.ensureIndex>`. See :doc:`indexes |
| 255 | +</core/indexes>` for an overview of indexes. |
| 256 | + |
| 257 | +The index specification documents contain field and value pairs, in |
| 258 | +the following form: |
| 259 | + |
| 260 | +.. code-block:: javascript |
| 261 | + |
| 262 | + { field: value } |
| 263 | + |
| 264 | +- ``field`` is the field in the documents to index. |
| 265 | + |
| 266 | +- ``value`` is either 1 for ascending or -1 for descending. |
| 267 | + |
| 268 | +The following document specifies the :ref:`multi-key index <index-type-multi-key>` on the ``_id`` |
| 269 | +field and the ``last`` field contained in the subdocument ``name`` |
| 270 | +field: |
| 271 | + |
| 272 | +.. code-block:: javascript |
| 273 | + |
| 274 | + { _id: 1, 'name.last': 1 } |
| 275 | + |
| 276 | +When passed as an argument to the :method:`ensureIndex() |
| 277 | +<db.collection.ensureIndex()>` method, the index documents specifies |
| 278 | +the index to create: |
| 279 | + |
| 280 | +.. code-block:: javascript |
| 281 | + |
| 282 | + db.csbios.ensureIndex( { _id: 1, 'name.last': 1 } ) |
| 283 | + |
| 284 | +.. _documents-sort-order: |
| 285 | + |
| 286 | +Sort Order Specification Documents |
| 287 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 288 | + |
| 289 | +The sort order documents specify the order of documents that a |
| 290 | +:method:`query() <db.collection.find()>` returns. Pass sort order |
| 291 | +specification documents as an argument to the :method:`sort() |
| 292 | +<cursor.sort()>` method. See the :method:`sort() <cursor.sort()>` page |
| 293 | +for more information on sorting. |
| 294 | + |
| 295 | +The sort order specifications contain field and value pairs, in the following |
| 296 | +form: |
| 297 | + |
| 298 | +.. code-block:: javascript |
| 299 | + |
| 300 | + { field: value } |
| 301 | + |
| 302 | +- ``field`` is the field by which to sort documents. |
| 303 | + |
| 304 | +- ``value`` is either 1 for ascending or -1 for descending. |
| 305 | + |
| 306 | +The following document specifies the sort order using the fields from a |
| 307 | +sub-document ``name`` first sort by the ``last`` field ascending, then |
| 308 | +by the ``first`` field also ascending: |
| 309 | + |
| 310 | +.. code-block:: javascript |
| 311 | + |
| 312 | + { 'name.last': 1, 'name.first': 1 } |
| 313 | + |
| 314 | +When passed as an argument to the :method:`sort() <cursor.sort()>` |
| 315 | +method, the sort order document sorts the results of the |
| 316 | +:method:`find() <db.collection.find()>` method: |
| 317 | + |
| 318 | +.. code-block:: javascript |
| 319 | + |
| 320 | + db.csbios.find().sort( { 'name.last': 1, 'name.first': 1 } ) |
0 commit comments