- FAQ: MongoDB Fundamentals
- What platforms does MongoDB support?
- Is MongoDB offered as a hosted service?
- How does a collection differ from a table?
- How do I create a database and a collection?
- How do I define or alter the collection schema?
- Does MongoDB support SQL?
- Does MongoDB support transactions?
- Does MongoDB handle caching?
- How does MongoDB address SQL or Query injection?
FAQ: MongoDB Fundamentals
On this page
- What platforms does MongoDB support?
- Is MongoDB offered as a hosted service?
- How does a collection differ from a table?
- How do I create a database and a collection?
- How do I define or alter the collection schema?
- Does MongoDB support SQL?
- Does MongoDB support transactions?
- Does MongoDB handle caching?
- How does MongoDB address SQL or Query injection?
This document answers some common questions about MongoDB.
What platforms does MongoDB support?
For the list of supported platforms, seeSupported Platforms.
Is MongoDB offered as a hosted service?
Yes.MongoDB Atlasis a cloud-hosted database-as-a-service. For more information, please visit theMongoDB Atlas docs.
How does a collection differ from a table?
Instead of tables, a MongoDB database stores its data incollections. A collection holds one or moreBSON documents. Documents are analogous to records or rows in a relational database table. Each document hasone or more fields; fields are similar to the columns in a relational database table.
SEE ALSO
SQL to MongoDB Mapping Chart,Introduction to MongoDB
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.[1]
As such, you can switch to a non-existent database (use<dbname>
) and perform the following operation:
use myNewDB
db.myNewCollection1.insertOne( { x: 1 } )
db.myNewCollection2.createIndex( { a: 1 } )
Theinsert
operation creates both the databasemyNewDB
and the collectionmyNewCollection1
if they do not already exist.
ThecreateIndex
operation, which occurs after themyNewDB
has been created, creates the index and the collectionmyNewCollection2
if the collection does not exist. IfmyNewDb
did not exist, thecreateIndex
operation would have also created themyNewDB
.
[1] | You can also create a collection explicitly usingdb.createCollection if you want to specify specific options, such as maximum size or document validation rules. |
---|---|
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.
Changed in version 3.2:Starting in MongoDB 3.2, however, you can enforcedocument validation rulesfor 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. Seedb.createCollection
andcollMod
. 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, seeMongoDB CRUD Operations
SEE ALSO
Does MongoDB support transactions?
MongoDB does not support multi-document transactions. However, MongoDB does provide atomic operations on a single document.
For more details on MongoDB’s isolation guarantees and behavior under concurrency, seeFAQ: Concurrency.
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, seeWiredTiger and Memory UseandMMAPv1 and Memory Use.
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 asBSONobjects. Typicallyclient librariesprovide a convenient, injection free, process to build these objects. Consider the following C++ example:
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"}
. Ifmy_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
You can disable all server-side execution of JavaScript, by passing the--noscripting
option on the command line or settingsecurity.javascriptEnabled
in a configuration file.
All of the following MongoDB operations permit you to run arbitrary JavaScript expressions directly on the server:
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 aBSONfield and pass JavaScript code to the$where
field.
If you need to pass user-supplied values in a$where
clause, you may escape these values with theCodeWScope
mechanism. When you set user-submitted values as variables in the scope document, you can avoid evaluating them on the database server.