Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

python mongodb schema

from pymongo import MongoClient
from pymongo.errors import CollectionInvalid
from collections import OrderedDict

db = MongoClient("mongodb://localhost:27019/")['mydatabase']

user_schema = {
    'firstName': {
        'type': 'string',
        'minlength': 1,
        'required': True,
    },
    'lastName': {
        'type': 'string',
        'minlength': 1,
        'required': True,
    },
    'email': {
        'type': 'string',
        "required": False,
    },
    'phoneNo': {
        'type': 'int',
        'required': True,
    },
    'userId': {
        'type': 'int',
        'required': True,
    },
    'patientId': {
        'type': 'int',
        'required': True,
    },
    'age': {
        'type': 'int'
    },
    "userStatus": {
        "type": "int"
    }
}

collection = 'Userinformation'
validator = {'$jsonSchema': {'bsonType': 'object', 'properties': {}}}
required = []

for field_key in user_schema:
    field = user_schema[field_key]
    properties = {'bsonType': field['type']}
    minimum = field.get('minlength')

    if type(minimum) == int:
        properties['minimum'] = minimum

    if field.get('required') is True: required.append(field_key)

    validator['$jsonSchema']['properties'][field_key] = properties

if len(required) > 0:
    validator['$jsonSchema']['required'] = required

query = [('collMod', collection),
         ('validator', validator)]

try:
    db.create_collection(collection)
except CollectionInvalid:
    pass

command_result = db.command(OrderedDict(query))
Comment

PREVIOUS NEXT
Code Example
Python :: how get 1st column in all rows of a 2d matrix in python 
Python :: python read file xlsx and return a list 
Python :: how to get the year and month in python 
Python :: def factorial python 
Python :: root value of a column pandas 
Python :: Class In Python With Instance Method 
Python :: How do you create an matrix of random integers in Numpy? 
Python :: cursor.fetchall() to list 
Python :: how take in put as list in integer value 
Python :: how to print class attributes in python 
Python :: install pocketsphinx error 
Python :: print colored text to console python 
Python :: pyqt tutorial 
Python :: python backslash in string 
Python :: how to make tkinter look better 
Python :: check if an object has an attribute in Python 
Python :: geodataframe change crs 
Python :: pthon return value with highest occurences 
Python :: indexes meta django 
Python :: Reason: Worker failed to boot 
Python :: dictionary python values 
Python :: django customize the user model 
Python :: Add Cog to bot in Discord.py 
Python :: concact geodataframe python 
Python :: django signals 
Python :: int to hex python without 0x 
Python :: Reverse an string Using Extended Slice Syntax in Python 
Python :: how to add labels on bar of barchart seaborn 
Python :: how to add reaction by message id in discord.py 
Python :: django login 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =