Docs HomeMongoDB Manual

FAQ: MongoDB Fundamentals常见问题解答:MongoDB基础知识

This document answers some common questions about MongoDB.本文档回答了一些关于MongoDB的常见问题。

What platforms does MongoDB support?MongoDB支持哪些平台?

For the list of supported platforms, see Platform Support.有关受支持平台的列表,请参阅平台支持

Is MongoDB offered as a hosted service?MongoDB是否作为托管服务提供?

Yes. MongoDB Atlas is a cloud-hosted database-as-a-service. 是一个云托管的数据库即服务。For more information, please visit MongoDB Atlas.有关更多信息,请访问MongoDB Atlas

How does a collection differ from a table?集合与表有何不同?

Instead of tables, a MongoDB database stores its data in collections. MongoDB数据库将数据存储在集合中,而不是表。A collection holds one or more BSON documents. 集合包含一个或多个BSON文档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?如何创建数据库和集合?

Note

You can enter the commands referenced in this FAQ by using the MongoDB Shell. 您可以使用MongoDB Shell输入本FAQ中引用的命令。The MongoDB Shell is an interactive JavaScript interface to MongoDB. You can use the MongoDB Shell to query and update data as well as perform administrative operations.MongoDB Shell是MongoDB的交互式JavaScript接口。您可以使用MongoDB Shell查询和更新数据以及执行管理操作。

If a database does not exist, MongoDB creates the database when you first store data for that database.如果数据库不存在,MongoDB会在您第一次存储该数据库的数据时创建该数据库。

If a collection does not exist, MongoDB creates the collection when you first store data for that collection.如果某个集合不存在,MongoDB会在您第一次存储该集合的数据时创建该集合。

As such, you can switch to a non-existent database (use <dbname>) and perform the following operation:因此,您可以切换到不存在的数据库(使用use <dbname>)并执行以下操作:

use myNewDB;

db.myNewCollection1.insertOne( { x: 1 } );
db.myNewCollection2.createIndex( { a: 1 } );

You can also create a collection explicitly using db.createCollection() method if you want to specify specific options, such as maximum size or document validation rules:如果要指定特定选项,例如最大大小或文档验证规则,也可以使用db.createCollection()方法显式创建集合:

use myNewDB;

db.createCollection("myNewCollection1");

How do I define or alter the collection schema?如何定义或更改集合架构?

You do not need to specify a schema for a collection in MongoDB. 您不需要为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在3.2版中更改: Starting in MongoDB 3.2, however, you can enforce document validation rules for a collection during update and insert operations.:但是,从MongoDB 3.2开始,您可以在更新和插入操作期间强制执行集合的文档验证规则

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. 请参见db.createCollection()collModIf 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.如果没有指定这些属性,则不需要显式创建集合,因为MongoDB在首次存储集合的数据时会创建新的集合。

Does MongoDB support SQL?MongoDB支持SQL吗?

Not directly, no. However, MongoDB does support a rich query language of its own. 不是直接的,不是。但是,MongoDB确实支持自己丰富的查询语言。For examples on using MongoDB's query language, see MongoDB CRUD Operations.有关使用MongoDB查询语言的示例,请参阅MongoDB CRUD操作

You can also use the MongoDB Connector for BI to query MongoDB collections with SQL.您还可以使用MongoDB Connector for BI来查询带有SQL的MongoDB集合。

If you are considering migrating your SQL application to MongoDB, download the MongoDB Application Modernization Guide for a best practices migration guide, reference schema, and other helpful resources.如果您正在考虑将SQL应用程序迁移到MongoDB,请下载MongoDB应用程序现代化指南,以获取最佳实践迁移指南、参考模式和其他有用的资源。

Does MongoDB support transactions?MongoDB支持事务吗?

Because a single document can contain related data that would otherwise be modeled across separate parent-child tables in a relational schema, MongoDB's atomic single-document operations already provide transaction semantics that meet the data integrity needs of the majority of applications. 由于单个文档可以包含相关数据,否则这些数据将在关系模式中跨单独的父子表进行建模,因此MongoDB的原子单文档操作已经提供了满足大多数应用程序数据完整性需求的事务语义。One or more fields may be written in a single operation, including updates to multiple sub-documents and elements of an array. 一个或多个字段可以在单个操作中写入,包括对多个子文档和数组元素的更新。The guarantees provided by MongoDB ensure complete isolation as a document is updated; any errors cause the operation to roll back so that clients receive a consistent view of the document.MongoDB提供的保证确保在文档更新时完全隔离;任何错误都会导致操作回滚,以便客户端接收到文档的一致视图。

However, for situations that require atomicity of reads and writes to multiple documents (in a single or multiple collections), MongoDB supports multi-document transactions:然而,对于需要对多个文档(在单个或多个集合中)进行原子性读写的情况,MongoDB支持多文档事务:

  • In version 4.0, MongoDB supports multi-document transactions on replica sets.在4.0版本中,MongoDB支持副本集上的多文档事务。
  • In version 4.2, MongoDB introduces distributed transactions, which adds support for multi-document transactions on sharded clusters and incorporates the existing support for multi-document transactions on replica sets.在4.2版本中,MongoDB引入了分布式事务,增加了对分片集群上多文档事务的支持,并整合了对副本集上多文档事务的现有支持。

    For details regarding transactions in MongoDB, see the transactions page.有关MongoDB中事务的详细信息,请参阅事务页面。

Important

In most cases, multi-document transaction incurs a greater performance cost over single document writes, and the availability of multi-document transactions should not be a replacement for effective schema design. 在大多数情况下,与单文档写入相比,多文档事务会产生更高的性能成本,并且多文档事务的可用性不应取代有效的模式设计。For many scenarios, the denormalized data model (embedded documents and arrays) will continue to be optimal for your data and use cases. 对于许多场景,非规范化数据模型(嵌入文档和数组)将继续是您的数据和用例的最佳选择。That is, for many scenarios, modeling your data appropriately will minimize the need for multi-document transactions.也就是说,对于许多场景,对数据进行适当建模将最大限度地减少对多文档事务的需求。

For additional transactions usage considerations (such as runtime limit and oplog size limit), see also Production Considerations.有关其他事务使用注意事项(如运行时限制和操作日志大小限制),请参阅生产注意事项

Does MongoDB handle caching?MongoDB处理缓存吗?

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将最近使用的数据保存在RAM中。如果您已经为查询创建了索引,并且您的工作数据集适合RAM,那么MongoDB将从内存中为所有查询提供服务。

MongoDB does not cache the query results in order to return the cached results for identical queries.MongoDB不会为了返回相同查询的缓存结果而缓存查询结果。

For more information on MongoDB and memory use, see WiredTiger and Memory Use.有关MongoDB和内存使用的更多信息,请参阅WiredTiger和内存使用

How does MongoDB address SQL or Query injection?MongoDB如何处理SQL或Query注入?

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中组装查询时,它会构建一个BSON对象,而不是字符串。因此,传统的SQL注入攻击不是问题。下面将介绍更多细节和一些细微差别。

MongoDB represents queries as BSON objects. MongoDB将查询表示为BSON对象。Typically client libraries provide a convenient, injection free, process to build these objects. Consider the following C++ example:通常,客户端库提供了一个方便的、无注入的过程来构建这些对象。考虑以下C++示例:

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" }. 在这里,my_query将具有一个值,例如{ 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.如果my_query包含特殊字符,例如:,:{,则该查询根本不匹配任何文档。例如,用户不能劫持查询并将其转换为删除。

JavaScript

Note

You can disable all server-side execution of JavaScript:您可以禁用JavaScript的所有服务器端执行:

The following MongoDB operations permit you to run arbitrary JavaScript expressions directly on the server:以下MongoDB操作允许您直接在服务器上运行任意JavaScript表达式:

You must exercise care in these cases to prevent users from submitting malicious JavaScript.在这种情况下,您必须小心防止用户提交恶意JavaScript。

Fortunately, you can express most operations in MongoDB without JavaScript.幸运的是,您可以在没有JavaScript的情况下在MongoDB中表达大多数操作。