Does Pymongo have validation rules built in?

别说谁变了你拦得住时间么 提交于 2021-01-21 09:46:53

问题


I am trying to validate an inserted document against a schema, and was trying to find a way to validate the inserted document.

There are libraries like MongoEngine that say they do the work, but is there a way to do document validation directly via pymongo ?


回答1:


The python driver docs are indeed a little light on how to use the db.command. Here is a complete working example:

from pymongo import MongoClient
from collections import OrderedDict
import sys

client = MongoClient()   # supply connection args as appropriate 
db = client.testX

db.myColl.drop()

db.create_collection("myColl")  # Force create!

#  $jsonSchema expression type is prefered.  New since v3.6 (2017):
vexpr = {"$jsonSchema":
  {
         "bsonType": "object",
         "required": [ "name", "year", "major", "gpa" ],
         "properties": {
            "name": {
               "bsonType": "string",
               "description": "must be a string and is required"
            },
            "gender": {
               "bsonType": "string",
               "description": "must be a string and is not required"
            },
            "year": {
               "bsonType": "int",
               "minimum": 2017,
               "maximum": 3017,
               "exclusiveMaximum": False,
               "description": "must be an integer in [ 2017, 3017 ] and is required"
            },
            "major": {
               "enum": [ "Math", "English", "Computer Science", "History", None ],
               "description": "can only be one of the enum values and is required"
            },
            "gpa": {
               "bsonType": [ "double" ],
               "minimum": 0,
               "description": "must be a double and is required"
            }
         }
  }
}

cmd = OrderedDict([('collMod', 'myColl'),
        ('validator', vexpr),
        ('validationLevel', 'moderate')])

db.command(cmd)

try:
    db.myColl.insert({"x":1})
    print "NOT good; the insert above should have failed."
except:
    print "OK. Expected exception:", sys.exc_info()    

try:
    okdoc = {"name":"buzz", "year":2019, "major":"Math", "gpa":3.8}
    db.myColl.insert(okdoc)
    print "All good."
except:
    print "exc:", sys.exc_info()    



回答2:


MongoDB supports document validation at the engine level so you'll pick it up via pymongo. You declare your "schema" (rules actually) to the engine. Here's a great place to start: https://docs.mongodb.com/manual/core/document-validation/




回答3:


You can make a separated JSON file for your Document Validations Schema, like this:

    {
      "collMod": "users",
      "validator": {
        "$jsonSchema": {
          "bsonType": "object",
          "required": ["email", "password","name"],
          "properties": {
            "email": {
              "bsonType": "string",
              "description": "Correo Electrónico"
            },
            "password": {
              "bsonType": "string",
              "description": "Una representación Hash de la contraseña"
            },
            "name": {
              "bsonType": "object",
              "required": ["first", "last"],
              "description": "Objeto que separa los nombres y apellidos",
              "properties":  {
                "first": {
                  "bsonType": "string",
                  "description": "Primer y segundo nombre"
                },
                "last": {
                  "bsonType": "string",
                  "description": "Primer y segundo apellido"
                }
              }
            },
          }
        }
      }
    }

Then you can use in python script, example:

from pymongo import MongoClient
import json #parse JSON  file as dict
from collections import OrderedDict #preserve the order (key, value) in the gived insertions on the dict

client = MongoClient("your_mongo_uri")
db = client.your_db_name 

with open('your_schema_file.json', 'r') as j:
    d = json.loads(j.read())

d = OrderedDict(d)

db.command(d)

OrderedDict Info

collMod Info

Schema Validation Info



来源:https://stackoverflow.com/questions/46569262/does-pymongo-have-validation-rules-built-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!