On this page本页内容
This document answers some common questions about MongoDB.本文回答了一些关于MongoDB的常见问题。
For the list of supported platforms, see Supported Platforms.有关支持的平台列表,请参阅支持的平台。
Yes. MongoDB Atlas is a cloud-hosted database-as-a-service. 对!MongoDB Atlas是一个云托管的数据库即服务。For more information, please visit MongoDB Atlas.有关更多信息,请访问MongoDB Atlas。
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.每个文档都有一个或多个字段;字段类似于关系数据库表中的列。
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. MongoDB Shell是MongoDB的一个交互式JavaScript接口。You can use the MongoDB Shell to query and update data as well as perform administrative operations.您可以使用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 } );
db.collection.insertOne()
method creates the collection myNewCollection1
if it does not already exist.myNewCollection1
不存在,则db.collection.insertOne()
方法会创建该集合。db.collection.createIndex()
method creates the index and the collection myNewCollection2
if it does not already exist.db.collection.createIndex()
方法创建索引和集合myNewCollection2
(如果它不存在)。myNewDb
database did not exist, either the db.collection.createIndex()
method or db.collection.insertOne()
method would have created the myNewDb
database automatically.myNewDb
数据库不存在,db.collection.createIndex()
方法或db.collection.insertOne()
方法都会自动创建myNewDb数据库。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");
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: Starting in MongoDB 3.2, however, you can enforce document validation rules for a collection during update and insert operations.在版本3.2中进行了更改:但是,从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()
和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.如果未指定这些属性,则无需显式创建集合,因为MongoDB在首次存储集合数据时会创建新集合。
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应用程序现代化指南,以获取最佳实践迁移指南、参考模式和其他有用的资源。
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.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中事务的详细信息,请参阅事务页面。
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.有关其他事务使用注意事项(如运行时限制和oplog大小限制),请参阅生产注意事项。
Yes. MongoDB keeps most recently used data in RAM. 对!MongoDB将最近使用的数据保存在RAM中。If you have created indexes for your queries and your working data set fits in RAM, MongoDB serves all queries from memory.如果您已经为查询创建了索引,并且工作数据集适合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和内存使用。
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. 当客户端程序在MongoDB中组装查询时,它会构建一个BSON对象,而不是一个字符串。因此,传统的SQL注入攻击不是问题。More details and some nuances are covered below.下面将介绍更多细节和一些细微差别。
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. my_query
包含特殊字符,例如,
、:
和{
,那么查询将与任何文档都不匹配。For example, users cannot hijack a query and convert it to a delete.例如,用户不能劫持查询并将其转换为删除。
You can disable all server-side execution of JavaScript:您可以禁用JavaScript的所有服务器端执行:
mongod
instance by passing the --noscripting
option on the command line or setting security.javascriptEnabled
to false in the configuration file.mongod
实例,通过在命令行上传递--noscripting
选项或在配置文件中将security.javascriptEnabled
设置为false
。Starting in MongoDB 4.4, for a 从MongoDB 4.4开始,对于mongos
instance by passing the --noscripting
option on the command line or setting security.javascriptEnabled
to false in the configuration file.mongos
实例,在命令行上传递--noscripting
选项,或者在配置文件中将security.javascriptEnabled
设置为false
。
mongos
instances.mongos
实例上执行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中表达大多数操作。