zoukankan      html  css  js  c++  java
  • Mongo Document 校验

    Mongo Datamodel Validation

    mongo insert,update document时候的校验规则

    指定validation rules

    • new collection
      db.createCollection(...,{validator:})
    • existing collection
      collMod command

    json schema (version >=3.6)

    $jsonSchema匹配满足指定的JSON Schema

    语法

    { $jsonSchema: <JSON Schema object> }

    例如

     db.createCollection("students", {
       validator: {
          $jsonSchema: {
             bsonType: "object",
             required: [ "name", "year", "major", "address" ],
             properties: {
                name: {
                   bsonType: "string",
                   description: "must be a string and is required"
                },
                year: {
                   bsonType: "int",
                   minimum: 2017,
                   maximum: 3017,
                   description: "must be an integer in [ 2017, 3017 ] and is required"
                },
                major: {
                   enum: [ "Math", "English", "Computer Science", "History", null ],
                   description: "can only be one of the enum values and is required"
                },
                gpa: {
                   bsonType: [ "double" ],
                   description: "must be a double if the field exists"
                },
                address: {
                   bsonType: "object",
                   required: [ "city" ],
                   properties: {
                      street: {
                         bsonType: "string",
                         description: "must be a string if the field exists"
                      },
                      city: {
                         bsonType: "string",
                         "description": "must be a string and is required"
                      }
                   }
                }
             }
          }
       }
    })
    

    其它查询表达式

    $near,$nearSphere,$text,$where

    db.createCollection( "contacts",
       { validator: { $or:
          [
             { phone: { $type: "string" } },
             { email: { $regex: /@mongodb.com$/ } },
             { status: { $in: [ "Unknown", "Incomplete" ] } }
          ]
       }
    } )
    

    行为

    validation 在insert,update 触发,collection新添加 validation,之前的document不会触发校验

    validationlevel

    validationlevel用来决定对于校验,mongo采用什么样的操作

    • strict(default)

    应用校验到所有insert,update

    • moderate

    应用校验到满足校验的document

    • off

    关闭校验

    validationaction

    validationaction是mongo用来决定在没有通过校验时做什么操作

    • error (default)

    拒绝所有违反校验的insert,update

    • warn

    mongo会记录下来但会让其通过校验

    不能在admin,local,config库和system.*集合中设置校验

  • 相关阅读:
    [2021.8集训Day10/JZOJ.3410]【GDOI2014模拟】Tree
    [2021.8集训Day10/JZOJ.3441]【NOIP2013模拟】小喵喵的新家
    [模板]模拟退火 / 洛谷 P1337 [JSOI2004]平衡点
    P1600 [NOIP2016 提高组] 天天爱跑步
    P4556 [Vani有约会]雨天的尾巴 /【模板】线段树合并
    selenium的三种等待
    python中socket、socketio、flask-socketio、WebSocket的区别与联系
    (十二)python3 迭代器
    (十一)python3 encode()和decode()
    (十)python3 生成器
  • 原文地址:https://www.cnblogs.com/szbnl/p/11245207.html
Copyright © 2011-2022 走看看