有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

mongodb向现有集合添加新验证程序

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

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" }的最佳方法是什么

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


共 (2) 个答案

  1. # 1 楼答案

    let previousValidator = db.getCollectionInfos({name: "collectionName"})[0].options.validator;
    # push the key to required array
    previousValidator.$jsonSchema.required.push("isBloodReportAvailable")
    
    let isBloodReportAvailabl = {"bsonType" : "bool", "description" : "must be an bool object and is optional" }
    # add new property to validator
    previousValidator1.$jsonSchema.properties['isBloodReportAvailable'] = isBloodReportAvailabl
    
    db.runCommand({
      "collMod": "collectionName",
      "validator": previousValidator,
    });
    
  2. # 2 楼答案

    在你的问题中,我“应该”可能会在前面加上一个误解。事实上,MongoDB不同于传统的RDBMS,因为它是“无模式的”,实际上根本不需要“创建字段”。因此,这与“表模式”不同,在“表模式”中,在模式更改之前,您无法执行任何操作。然而,“验证”是另一回事,也是写作中“仍然”相对较新的特征

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

    现有文件

    documentation中所述,该集合具有现有文档

    Existing Documents

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

    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()上的选项外,您还可以使用文档修改现有集合,但应“小心”当前文档可能不符合所需规则。因此,如果您不确定集合中的所有文档是否符合规则,请使用“中等”

    为了应用,您目前使用^{}方法发出设置验证规则的“命令”。这是上文中的“验证级别”

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

    let validatior = db.getCollectionInfos({name: "MyTestCollection"})[0].options.validator;
    
    validator.$or.push({ "LastLoginDate": { "$type": "date" } });
    
    db.runCommand({
      "collMod": "MyTestCollection",
      "validator": validator,
      "validationLevel": "moderate"
    });
    

    当然,如前所述,如果您确信所有文档都满足条件,则可以将“strict”应用为默认值

    空集合

    如果在这种情况下,集合实际上是“空的”,没有任何文档,您可以“删除”集合,因为当前数据不重要,那么您可以简单地改变上述内容,并将.createCollection().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 });