- Frequently Asked Questions >
- FAQ: MongoDB Fundamentals
FAQ: MongoDB Fundamentals¶
On this page
This document answers some common questions about MongoDB.
How does a collection differ from a table?¶
Instead of tables, a MongoDB database stores its data in collections. A collection holds one or more BSON documents. Documents are analogous to records or rows in a relational database table. Each document has one or more fields; fields are similar to the columns in a relational database table.
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.insert( { 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.
[1] | You can also create a collection explicitly using db.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 enforce document validation rules 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 db.createCollection and 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 MongoDB CRUD Tutorials
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, see FAQ: 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, see WiredTiger and Memory Use and MMAPv1 and Memory Use.
Thank you for your feedback!
We're sorry! You can Report a Problem to help us improve this page.