/
fundamentals.txt
193 lines (136 loc) · 6.43 KB
/
fundamentals.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
=========================
FAQ: MongoDB Fundamentals
=========================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
This document answers some common questions about MongoDB.
What platforms does MongoDB support?
------------------------------------
For the list of supported platforms, see
:ref:`prod-notes-supported-platforms`.
Is MongoDB offered as a hosted service?
---------------------------------------
Yes. `MongoDB Atlas <https://www.mongodb.com/cloud/atlas?jmp=docs>`_ is
a cloud-hosted database-as-a-service. For more information, please
visit the `MongoDB Atlas docs
<https://www.mongodb.com/cloud/atlas?jmp=docs>`_.
How does a collection differ from a table?
------------------------------------------
Instead of tables, a MongoDB database stores its data in
:term:`collections <collection>`. A collection holds one or more
:ref:`BSON documents <bson-document-format>`. Documents are analogous
to records or rows in a relational database table. Each document has
:ref:`one or more fields <document-structure>`; fields are similar to
the columns in a relational database table.
.. seealso:: :doc:`/reference/sql-comparison`, :doc:`/introduction`
How do I create a database and a collection?
--------------------------------------------
If a database does not exist, MongoDB creates the database when you
first store data for that database.
If a collection does not exist, MongoDB creates the collection when you
first store data for that collection. [#explicit-creation]_
As such, you can switch to a non-existent database (``use <dbname>``)
and perform the following operation:
.. code-block:: javascript
use myNewDB
db.myNewCollection1.insertOne( { x: 1 } )
db.myNewCollection2.createIndex( { a: 1 } )
The ``insert`` operation creates both the database ``myNewDB`` and the
collection ``myNewCollection1`` if they do not already exist.
The ``createIndex`` operation, which occurs after the ``myNewDB`` has
been created, creates the index and the collection ``myNewCollection2``
if the collection does not exist. If ``myNewDb`` did not exist, the
``createIndex`` operation would have also created the ``myNewDB``.
.. [#explicit-creation]
You can also create a collection explicitly using
:method:`db.createCollection` if you want to specify specific
options, such as maximum size or document validation rules.
.. _faq-schema-free:
How do I define or alter the collection schema?
-----------------------------------------------
You do not need to specify a schema for a collection in MongoDB.
Although it is common for the documents in a collection to have a
largely homogeneous structure, it is not a requirement; i.e. documents
in a single collection do not need to have the same set of fields. The
data type for a field can differ across documents in a collection as
well.
To change the structure of the documents in a collection, update the
documents to the new structure. For instance, add new fields, remove
existing ones, or update the value of a field to a new type.
.. versionchanged:: 3.2
Starting in MongoDB 3.2, however, you can enforce :doc:`document
validation rules </core/document-validation>` for a collection
during update and insert operations.
Some collection properties, such as specifying a maximum size, can be
specified during the explicit creation of a collection and be modified.
See :method:`db.createCollection` and :dbcommand:`collMod`. If you are
not specifying these properties, you do not need to explicitly create
the collection since MongoDB creates new collections when you first
store data for the collections.
Does MongoDB support SQL?
-------------------------
No. However, MongoDB does support a rich query language of its own. For
examples on using MongoDB's query language, see
:doc:`/crud`
.. seealso:: :doc:`/reference/sql-comparison`
Does MongoDB support transactions?
----------------------------------
.. include:: /includes/extracts/transactions-faq.rst
.. include:: /includes/extracts/transactions-usage.rst
.. note::
.. include:: /includes/fact-upcoming.rst
.. _faq-database-and-caching:
Does MongoDB handle caching?
----------------------------
Yes. MongoDB keeps most recently used data in RAM. If you have created
indexes for your queries and your working data set fits in RAM, MongoDB
serves all queries from memory.
MongoDB does not cache the query results in order to return the cached
results for identical queries.
For more information on MongoDB and memory use, see :ref:`WiredTiger
and Memory Use <wiredtiger-RAM>` and :ref:`MMAPv1 and Memory Use
<mmapv1-RAM>`.
.. _faq-dollar-sign-escaping:
How does MongoDB address SQL or Query injection?
------------------------------------------------
BSON
~~~~
As a client program assembles a query in MongoDB, it builds a BSON
object, not a string. Thus traditional SQL injection attacks are not a
problem. More details and some nuances are covered below.
MongoDB represents queries as :term:`BSON` objects. Typically
:ecosystem:`client libraries </drivers>` provide a convenient,
injection free, process to build these objects. Consider the following
C++ example:
.. code-block:: cpp
BSONObj my_query = BSON( "name" << a_name );
auto_ptr<DBClientCursor> cursor = c.query("tutorial.persons", my_query);
Here, ``my_query`` then will have a value such as ``{ name : "Joe"
}``. If ``my_query`` contained special characters, for example
``,``, ``:``, and ``{``, the query simply wouldn't match any
documents. For example, users cannot hijack a query and convert it to
a delete.
JavaScript
~~~~~~~~~~
.. note::
.. include:: /includes/fact-disable-javascript-with-noscript.rst
All of the following MongoDB operations permit you to run arbitrary JavaScript
expressions directly on the server:
- :query:`$where`
- :dbcommand:`mapReduce`
- :dbcommand:`group`
You must exercise care in these cases to prevent users from
submitting malicious JavaScript.
Fortunately, you can express most queries in MongoDB without
JavaScript and for queries that require JavaScript, you can mix
JavaScript and non-JavaScript in a single query. Place all the
user-supplied fields directly in a :term:`BSON` field and pass
JavaScript code to the :query:`$where` field.
If you need to pass user-supplied values in a :query:`$where` clause,
you may escape these values with the ``CodeWScope`` mechanism. When you
set user-submitted values as variables in the scope document, you can
avoid evaluating them on the database server.