Frequently Asked Questions:
This document addresses basic high level questions about MongoDB and it’s use.
If you don’t find the answer you’re looking for, check the complete list of FAQs or post your question to the MongoDB User Mailing List.
MongoDB is a document-oriented DBMS. Think of MySQL but with JSON-like objects comprising the data model, rather than RDBMS tables. Significantly, MongoDB supports neither joins nor transactions. However, it features secondary indexes, an expressive query language, atomic writes on a per-document level, and fully-consistent reads.
操作上,MongoDB 采用主从复制,具有自动故障转移功能,并通过基于范围的自动分割实现内置水平缩放。
Instead of tables, a MongoDB database stores its data in collections, which are the rough equivalent of RDBMS tables. A collection holds one or more documents, which corresponds to a record or a row in a relational database table, and each document has one or more fields, which corresponds to a column in a relational database table.
Collections have important differences from RDBMS tables. Documents in a single collection may have a unique combination and set of fields. Documents need not have identical fields. You can add a field to some documents in a collection without adding that field to all documents in the collection.
MongoDB uses dynamic schemas. You can create collections without defining the structure, i.e. the fields or the types of their values, of the documents in the collection. You can change the structure of documents simply by adding new fields or deleting existing ones. Documents in a collection need not have an identical set of fields.
In practice, it is common for the documents in a collection to have a largely homogeneous structure; however, this is not a requirement. MongoDB’s flexible schemas mean that schema migration and augmentation are very easy in practice, and you will rarely, if ever, need to write scripts that perform “alter table” type operations, which simplifies and facilitates iterative software development with MongoDB.
MongoDB client drivers exist for all of the most popular programming languages, and many other ones. See the latest list of drivers for details.
MongoDB 采用通用设计,适合大量应用情形,例如:内容管理系统、移动应用、游戏、电子商务、分析、存档和日志等。
MongoDB 不适用于需要 SQL、联接和多对象事务处理的系统。
MongoDB 未提供 ACID 事务。
然而,MongoDB 提供了一些基本事务处理能力。原子操作在单个文档范围内是可行的。例如,如果“a”和“b”是同一文档内的两个字段,那么就可以将借记 a 和贷记 b 当作事务处理。文档可以非常丰富,因此某些文档可能包含成千上万个字段,并支持测试子文档中的字段。
此外,您可以在 MongoDB 持久(ACID 中的‘D’)中执行写操作。要获得持久写操作,必须启用日志,64位版本中日志默认开启。您还必须发出写入关注为 {j: true} 的写操作,确保日记之前的写操作块已与磁盘同步。
Users have built successful e-commerce systems using MongoDB, but applications requiring multi-object commits with rollback generally aren’t feasible.
不一定。只有少量空闲 RAM 的机器当然也可以运行 MongoDB。
MongoDB 自动将机器上的所有可用内存用作缓存。系统资源监视器显示 MongoDB 使用了大量内存,但其使用是动态的。如果另一个进程突然需要服务器的一半 RAM,MongoDB 会将用作缓存的内存交给该进程使用。
技术上,操作系统的虚拟内存子系统管理 MongoDB 的内存。这意味着 MongoDB 将使用它能够使用的所有可用内存,并根据需要交换到磁盘。提供足够的内存以支持应用程序在 RAM 中的工作数据集,将能实现最佳性能。
另见
FAQ: MongoDB Diagnostics for answers to additional questions about MongoDB and Memory use.
MongoDB has no configurable cache. MongoDB uses all free memory on the system automatically by way of memory-mapped files. Operating systems use the same approach with their file system caches.
No. In MongoDB, a document’s representation in the database is similar to its representation in application memory. This means the database already stores the usable form of data, making the data usable in both the persistent store and in the application cache. This eliminates the need for a separate caching layer in the application.
This differs from relational databases, where caching data is more expensive. Relational databases must transform data into object representations that applications can read and must store the transformed data in a separate cache: if these transformation from data to application objects require joins, this process increases the overhead related to using the database which increases the importance of the caching layer.
是的。MongoDB 将所有最近使用的数据保存在 RAM 中。 如果您为查询创建了索引,并且您的工作数据集能放进 RAM 中, MongoDB 将从内存响应所有查询。
MongoDB 不采用查询缓存;MongoDB 直接从索引和/或数据文件响应所有查询。
Writes are physically written to the journal within 100 milliseconds, by default. At that point, the write is “durable” in the sense that after a pull-plug-from-wall event, the data will still be recoverable after a hard restart. See journalCommitInterval for more information on the journal commit window.
While the journal commit is nearly instant, MongoDB writes to the data files lazily. MongoDB may wait to write data to the data files for as much as one minute by default. This does not affect durability, as the journal has enough information to ensure crash recovery. To change the interval for writing to the data files, see syncdelay.
MongoDB is implemented in C++. Drivers and client libraries are typically written in their respective languages, although some drivers use C extensions for better performance.
MongoDB uses memory-mapped files. When running a 32-bit build of MongoDB, the total storage size for the server, including data and indexes, is 2 gigabytes. For this reason, do not deploy MongoDB to production on 32-bit machines.
如果运行 64 位版本的 MongoDB,存储大小几乎无限制。对于生产部署,强烈建议使用 64 位版本和操作系统。
注意
32-bit builds disable journaling by default because journaling further limits the maximum amount of data that the database can store.