On this page本页内容
collMod
collMod
makes it possible to add options to a collection or to modify view definitions.可以向集合添加选项或修改视图定义。
The command takes the following prototype form:该命令采用以下原型形式:
noPadding
and usePowerOf2Sizes
for collMod
.collMod
删除了MMAPv1存储引擎和MMAPv2特定选项noPadding
和usePowerOf2Sizes
。pipeline
cannot include the $out
or the $merge
stage. pipeline
不能包含$out
或$merge
阶段。$lookup
or $facet
stage), this restriction applies to the nested pipelines as well.$lookup
或$facet
阶段),则此限制也适用于嵌套管道。db.runCommand( { collMod: <collection or view>, <option1>: <value1>, <option2>: <value2> ... } )
For the 对于<collection or view>
, specify the name of a collection or view in the current database.<collection or view>
,请指定当前数据库中的集合或视图的名称。
index
The index
option can change the following properties of an existing index:index
选项可以更改现有索引的以下属性:
Index Property | |
---|---|
expireAfterSeconds |
|
hidden |
|
To change index options, specify either the key pattern or name of the existing index and the index option or options you wish to change:要更改索引选项,请指定现有索引的键模式或名称以及要更改的索引选项:
db.runCommand( { collMod: <collection>, index: { keyPattern: <index_spec> || name: <index_name>, expireAfterSeconds: <number>, // If changing the TTL expiration threshold hidden: <boolean> // If changing the visibility of the index from the query planner } } )
If the index does not exist, the command errors with the message 如果索引不存在,则命令会出现错误,并显示消息"cannot find index <name|keyPattern> for ns <db.collection>"
."cannot find index <name|keyPattern> for ns <db.collection>"
。
validator
validator
allows users to specify validation rules or expressions for a collection. 允许用户为集合指定验证规则或表达式。For more information, see Schema Validation.有关详细信息,请参阅架构验证。
The validator
option takes a document that specifies the validation rules or expressions. validator
选项接受指定验证规则或表达式的文档。You can specify the expressions using the same operators as the query operators with the exception of 可以使用与查询运算符相同的运算符指定表达式,但$near
, $nearSphere
, $text
, and $where
.$near
、$nearSphere
、$text
和$where
除外。
admin
, local
, and config
databases.admin
、local
和config
数据库中的集合指定验证器。system.*
collections.system.*
集合指定validator
。validationLevel
The validationLevel
determines how strictly MongoDB applies the validation rules to existing documents during an update.validationLevel
确定MongoDB在更新期间对现有文档应用验证规则的严格程度。
validationLevel | |
---|---|
"off" | |
"strict" | Default |
"moderate" |
validationAction
The validationAction
option determines whether to error
on invalid documents or just warn
about the violations but allow invalid documents.validationAction
选项确定是在无效文档上error
,还是仅warn
违规但允许无效文档。
Validation of documents only applies to those documents as determined by the 文件验证仅适用于由validationLevel
.validationLevel
确定的那些文件。
validationAction | |
---|---|
"error" | Default |
"warn" |
To view the validation specifications for a collection, use the 要查看集合的验证规范,请使用db.getCollectionInfos()
method.db.getCollectionInfos()
方法。
viewOn
The underlying source collection or view for the view. 视图的基础源集合或视图。The view definition is determined by applying the specified 通过将指定的pipeline
to this source.pipeline
应用于此源来确定视图定义。
Required if modifying a view on a MongoDB deployment that is running with access control.如果修改使用访问控制运行的MongoDB部署上的视图,则为必填项。
pipeline
The aggregation pipeline that defines the view.定义视图的聚合管道。
The view definition 视图定义pipeline
cannot include the $out
or the $merge
stage. pipeline
不能包含$out
或$merge
阶段。If the view definition includes nested pipeline (e.g. the view definition includes 如果视图定义包含嵌套管道(例如,视图定义包含$lookup
or $facet
stage), this restriction applies to the nested pipelines as well.$lookup
或$facet
阶段),则此限制也适用于嵌套管道。
Required if modifying a view on a MongoDB deployment that is running with access control.如果修改使用访问控制运行的MongoDB部署上的视图,则为必填项。
The view definition is public; i.e. 视图定义是公共的;db.getCollectionInfos()
and explain
operations on the view will include the pipeline that defines the view. db.getCollectionInfos()
和explain
视图上的操作将包括定义视图的管道。As such, avoid referring directly to sensitive fields and values in view definitions.因此,避免直接引用视图定义中的敏感字段和值。
db.runCommand( { collMod: "myView", viewOn: "activities", pipeline: [ { $match: { status: "Q" } }, { $project: { user: 1, date: 1, description: 1} } ] } )
To enable automatic removal of documents or change the 要启用自动删除文档或更改现有时间序列集合的expireAfterSeconds
parameter value for an existing time series collection, issue the following collMod
command:expireAfterSeconds
参数值,请发出以下collMod
命令:
db.runCommand({ collMod: <collection>, expireAfterSeconds: <Number> || "off" })
The expireAfterSeconds
field must be either:expireAfterSeconds
字段必须是:
>=0
)>=0
)"off"
."off"
。A number specifies the number of seconds after which documents expire. 数字指定文档过期后的秒数。The string 字符串"off"
removes the expireAfterSeconds
parameter and disables automatic removal."off"
删除expireAfterSeconds
参数并禁用自动删除。
Optional. 可选。A document expressing the write concern of the 表示drop
command.drop
命令的写入问题的文档。
Omit to use the default write concern.省略以使用默认写入关注点。
If the deployment enforces authentication/authorization, you must have the following privilege to run the 如果部署强制验证/授权,您必须具有以下权限才能运行collMod
command:collMod
命令:
collMod in the databasecollMod | |
|
The built-in role 内置角色dbAdmin
provides the required privileges.dbAdmin
提供所需的权限。
The collMod
command obtains an exclusive lock on the parent database of the specified collection for the duration of the operation. collMod
命令在操作期间获取指定集合的父数据库上的独占锁。All subsequent operations on the database and all its collections must wait until 对数据库及其所有集合的所有后续操作必须等待collMod
releases the lock.collMod
释放锁。
The following example updates the 以下示例更新名为expireAfterSeconds
property of an existing TTL index { lastAccess: 1 }
on a collection named user_log
. user_log
的集合上现有TTL索引{ lastAccess: 1 }
的expireAfterSeconds
属性。The current 索引的当前expireAfterSeconds
property for the index is set to 1800
seconds (or 30 minutes) and the example changes the value to 3600
seconds (or 60 minutes).expireAfterSeconds
属性设置为1800
秒(或30分钟),示例将该值更改为3600
秒(或60分钟)。
db.runCommand({ collMod: "user_log", index: { keyPattern: { lastAccess: 1 }, expireAfterSeconds: 3600 } })
If successful, the operation returns a document that includes both the old and new value for the changed property:如果成功,该操作将返回一个文档,其中包含已更改属性的旧值和新值:
{ "expireAfterSeconds_old" : 1800, "expireAfterSeconds_new" : 3600, "ok" : 1 }
To hide an index, you must have featureCompatibilityVersion set to 要隐藏索引,必须将4.4
or greater. featureCompatibilityVersion
设置为4.4
或更高。However, once hidden, the index remains hidden even with featureCompatibilityVersion set to 然而,一旦隐藏,即使在MongoDB 4.4二进制文件上将4.2
on MongoDB 4.4 binaries.featureCompatibilityVersion
设置为4.2
,索引也会保持隐藏状态。
The following example hides an existing index on the 下面的示例隐藏了订单集合上的现有索引。orders
collection. Specifically, the operation hides the index with the specification 具体地说,该操作在查询计划器中隐藏规范为{ shippedDate: 1 }
from the query planner.{ shippedDate: 1 }
的索引。
db.runCommand({ collMod: "orders", index: { keyPattern: { shippedDate: 1 }, hidden: true } })
If successful, the operation returns a document that includes both the old and new value for the changed property:如果成功,该操作将返回一个文档,其中包含已更改属性的旧值和新值:
{ "hidden_old" : false, "hidden_new" : true, "ok" : 1 }
If the operation is successful but the 如果操作成功,但hidden
value has not changed (i.e. hiding an already hidden index or unhiding an already unhidden index), the command omits the hidden_old
and hidden_new
fields from the output.hidden
值未更改(即隐藏已隐藏的索引或取消隐藏已取消隐藏的索引),该命令将从输出中省略hidden_old
和hidden_new
字段。
To hide a text index, you must specify the index by 要隐藏文本索引,必须按name
and not by keyPattern
.name
而不是按keyPattern
指定索引。
The following example adds a validator to an existing collection named 下面的示例将验证程序添加到名为contacts
.contacts
的现有集合中。
MongoDB 3.6 adds the MongoDB 3.6添加了$jsonSchema
operator to support JSON Schema validation.$jsonSchema
运算符以支持JSON模式验证。
db.runCommand( { collMod: "contacts", validator: { $jsonSchema: { bsonType: "object", required: [ "phone" ], properties: { phone: { bsonType: "string", description: "must be a string and is required" }, email: { bsonType : "string", pattern : "@mongodb\.com$", description: "must be a string and match the regular expression pattern" }, status: { enum: [ "Unknown", "Incomplete" ], description: "can only be one of the enum values" } } } }, validationLevel: "moderate", validationAction: "warn" } )
With the 通过适度的moderate
validationLevel
, MongoDB applies validation rules to insert operations and to update operationss to existing documents that already fulfill the validation criteria. validationLevel
,MongoDB将验证规则应用于插入操作和更新已满足验证标准的现有文档的操作。Updates to existing documents that do not fulfill the validation criteria are not checked for validity.对不符合验证标准的现有文档的更新不会进行有效性检查。
With the 通过warn
validationAction
, MongoDB logs any violations but allows the insertion or update to proceed.warn
validationAction
,MongoDB记录任何违规行为,但允许继续插入或更新。
For example, the following insert operation violates the validation rule.例如,以下插入操作违反了验证规则。
db.contacts.insertOne( { name: "Amanda", status: "Updated" } )
However, since the 但是,由于validationAction
is warn
only, MongoDB only logs the validation violation message and allows the operation to proceed:validationAction
仅为warn
,MongoDB仅记录验证违规消息并允许操作继续:
2017-12-01T12:31:23.738-05:00 W STORAGE [conn1] Document would fail validation collection: example.contacts doc: { _id: ObjectId('5a2191ebacbbfc2bdc4dcffc'), name: "Amanda", status: "Updated" }
For more information, see Schema Validation.有关详细信息,请参阅架构验证。