Database Manual / Reference

DDL OperationsDDL操作

DDL (Data Description Language) operations change the properties of a database or collection. MongoDB supports both Explicit DDL Operations and Implicit DDL Operations. DDL(数据描述语言)操作更改数据库或集合的属性。MongoDB支持显式DDL操作隐式DDL操作Explicit DDL operations directly run an operation like creating or dropping a collection or index. Implicit DDL operations create collections by referencing a non-existent collection, like inserting data into a non-existent collection.显式DDL操作直接运行创建或删除集合或索引等操作。隐式DDL操作通过引用不存在的集合来创建集合,例如将数据插入不存在的集合中。

Explicit DDL Operations显式DDL操作

MongoDB supports the following explicit DDL operations:MongoDB支持以下显式DDL操作:

Implicit DDL Operations隐式DDL操作

MongoDB also supports write operations such as insert or update with upsert:true. MongoDB还支持写操作,如insert或使用upsert:trueupdateAny command that writes to a non-existing collection creates that collection.任何写入不存在的集合的命令都会创建该集合。

Examples示例

For example, this insert command creates the users collection if it does not already exist.例如,如果users集合不存在,则此insert命令会创建该集合。

db.runCommand(
{
insert: "users",
documents: [ { _id: 1, user: "abc123", status: "A" } ]
}
)

This update command with upsert: true creates the people collection if it does not already exist.如果people集合不存在,则此带有upsert:true的更新命令将创建该集合。

db.runCommand(
{
update: "people",
updates: [
{ q: { name: "Andy" }, u: { $inc: { score: 1 } }, upsert: true }
]
}
)