javascript 如何在MongoDb中添加集合验证?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 如何在MongoDb中添加集合验证?相关的知识,希望对你有一定的参考价值。

db.createCollection("collectionName", {
  validator: {
    $jsonSchema: {
      bsonType: "object", // everything in the collection should be a valid Mongo Document
      required: ["fileld1", "fiel2""],
      properties: {
        fileld1: {
          bsonType: "bsonType",
          description: "description"
        }
        fileld2: {
          bsonType: "object",
          description: "description"
          required: ["fileld1", "fiel2""],
          properties: { ...
        }
      }
    }
  }
}

db.createCollection("usersToSendEmail", {
  validator: {
    $jsonSchema: {
      bsonType: "object", // everything in the collection should be a valid Mongo Document
      required: ["name", "lastName","email", "birth"],
      properties: {
        name: {
          bsonType: "string",
          description: "Must be a string and is required"
        },
        birth: {
          bsonType: "date",
          description: "Must be a date type and is required"
        }
      }
    }
  }
})

以上是关于javascript 如何在MongoDb中添加集合验证?的主要内容,如果未能解决你的问题,请参考以下文章

亲测教你如何搭建 MongoDB 复制集 + 选举原理

MongoDB副本集replica set--添加删除成员

mongodb集群监控和运维

Mongodb副本集管理

mongodb 添加复制集

如何从外部访问托管在 kubernetes 集群上的 mongodb 副本集?