将新的验证器添加到现有集合 [英] Add new Validator to Existing Collection

查看:59
本文介绍了将新的验证器添加到现有集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将新字段(日期类型为LastLoginDate)添加到现有集合中.这是我的示例脚本:

I'm trying to add new field (LastLoginDate of type Date) to a existing collection. Here is my sample script:

db.createCollection( "MyTestCollection",
   { "validator": { "$or":
       [
          { "username": { "$type": "string" } },
          { "notes": { "$type": "string" } }
       ]
    }
   }
)

db.getCollectionInfos({name: "MyTestCollection"});
  [
     {
        "name" : "MyTestCollection",
        "options" : {
           "validator" : {
              "$or" : [
                 {
                    "username" : {
                       "$type" : "string"
                    }
                 },
                 {
                    "notes" : {
                       "$type" : "string"
                    }
                 }
              ]
           }
        }
     }
  ]

向此现有集合"MyTestCollection"中添加新字段LastLoginDate : { $type: "date" }的最佳方法是什么.

What is the best way to add new field LastLoginDate : { $type: "date" }, to this existing collection "MyTestCollection".

添加新文档或使用新字段更新现有集合可能会创建此字段.但是我不确定如何在新字段上强制执行日期类型.添加新文件后,如果我再次执行以下命令,则不会显示新添加字段的类型验证器.

Adding new document or updating existing collection with new field may create this field. But i'm not sure how to enforce the date type on the new field. After adding new filed, if i execute the following command again, it doesn't show type validator for newly added field.

推荐答案

我应该"在您的问题中加上一个误解.事实是,MongoDB与传统RDBMS的不同之处在于,它是无模式的",实际上您根本不需要创建字段".因此,这与表模式"不同,在表模式"中,除非更改模式,否则您将无法执行任何操作.但是,验证"是一回事,也是写作时仍然"相对较新的功能.

I "should" probably prefix this with one misconception in your question. The fact is MongoDB differs from traditional RDBMS in that it is "schemaless" and you do not in fact need to "create fields" at all. So this differs from a "table schema" where you cannot do anything until the schema changes. "Validation" however is a different thing as well as a "still" relatively new feature as of writing.

如果您要添加验证规则" ,则有些方法取决于集合的当前状态.在这两种情况下,实际上都没有添加到"功能,但是操作是将验证规则全部替换"为要指定的新规则.继续阅读有关其工作原理的规则.

If you want to "add a validation rule" then there are methods which depend on the current state of the collection. In either case, there actually is no "add to" function, but the action instead is to "replace" all the validation rules with new ones to specify. Read on for the rules of how this works.

文档

现有文档

您可以使用 validationLevel 选项控制MongoDB如何处理现有文档.

You can control how MongoDB handles existing documents using the validationLevel option.

默认情况下, validationLevel strict ,并且MongoDB将验证规则应用于所有插入和更新.将 validationLevel 设置为适中会将验证规则应用于满足验证条件的现有文档的插入和更新.在中等级别下,不检查对不满足验证条件的现有文档的更新.

By default, validationLevel is strict and MongoDB applies validation rules to all inserts and updates. Setting validationLevel to moderate applies validation rules to inserts and to updates to existing documents that fulfill the validation criteria. With the moderate level, updates to existing documents that do not fulfill the validation criteria are not checked for validity.

本部分和以下示例部分的基本意思是,除了.createCollection()上的选项之外,您还可以使用文档修改现有的集合,但应警惕"当前的文档可能不符合要求的规则.因此,如果不确定不确定集合中所有文档是否符合规则,请使用中等" .

This and the following example section are basically saying that in addition to the options on .createCollection() you may also modify an existing collection with documents, but should be "wary" that the present documents may not meet the required rules. Therefore use "moderate" if you are unsure the rule will be met for all documents in the collection.

要申请,请使用 .runCommand() 当前的方法发出设置验证规则的命令".上面段落中的"validationLevel" .

由于您已有规则,因此我们可以使用`.getCollectionInfos() 检索它们,然后添加新规则并应用:

Since you have existing rules, we can use the `.getCollectionInfos() to retrieve them and then add the new rule and apply:

let validatior = db.getCollectionInfos({name: "MyTestCollection"})[0].options.validator;

validator.$or.push({ "LastLoginDate": { "$type": "date" } });

db.runCommand({
  "collMod": "MyTestCollection",
  "validator": validator,
  "validationLevel": "moderate"
});

当然,如前所述,如果您确信所有文档均符合条件,则可以将"strict" 用作默认设置.

Of course as noted before, that if you are confident the documents all meet the conditions then you can apply "strict" as the default instead.

如果情况是集合实际上是空的",根本没有文档,则,您可以删除"集合,因为当前数据无关紧要,那么您可以简单地进行更改并结合使用.createCollection().drop():

If in the case is that the collection is actually "empty" with no documents at all or you may "drop" the collection since the current data is not of consequence, then you can simply vary the above and use .createCollection() in combination with .drop():

let validatior = db.getCollectionInfos({name: "MyTestCollection"})[0].options.validator;

validator.$or.push({ "LastLoginDate": { "$type": "date" } });

db.getCollection("MyTestCollection").drop();

db.createCollection( "MyTestCollection", { "validator": validator });

这篇关于将新的验证器添加到现有集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆