更新 mongodb atlas 数据库架构

update mongodb atlas database schema

当我创建一个集合并添加如下模型时,我是否可以通过 cli 进行更改?或者在创建一个集合后添加一个?

问题:创建后如何更新数据model/how添加模型

我试过 db.students.update,但没有用。

db.createCollection("students", {
   validator: {
      $jsonSchema: {
          bsonType: "object",
          required: [ "name", "year", "major", "gpa", "address.city", "address.street" ],        
                    }
               }
     })

它不是 数据库 模式。这是 validation 架构。请学习区别:https://docs.mongodb.com/manual/core/schema-validation/index.html.

在页面上的其他重要信息中,以下句子准确地回答了您的问题:

Validation occurs during updates and inserts. When you add validation to a collection, existing documents do not undergo validation checks until modification.

您需要自行处理现有文档以确保它们符合要求。

To add document validation to an existing collection, use collMod command with the validator option.

同样适用于现有验证规则的更新。在你的情况下,命令应该是这样的:

db.runCommand({ collMod: "students", "validator": {"$jsonSchema" : <your new schema> } })