<jupyter_start><jupyter_text>Schema's in MongoDB MongoDB database: collections met documentenCollection: documenten "van dezelfde soort"Document bepaalt **eigen schema**: namen en types van velden *Hoe kunnen we afdwingen dat documenten in een collection **hetzelfde schema** (namen, types) hebben?*<jupyter_code>import os
import re
import pandas as pd
import numpy as np
from IPython.core.display import display, HTML
import pymongo

os.environ["PATH"]=os.environ["PATH"] + ":/usr/local/bin"
pd.set_option('max_colwidth',160)

userline = !echo $USER
username = userline[0]
dbname = username + "-demodb"
print("Database name: " + dbname)<jupyter_output>Database name: eelco-demodb<jupyter_text>Initialisatie van de database. Het resultaat moet 0 zijn.<jupyter_code>from pymongo import MongoClient
print('Mongo version', pymongo.__version__)
client = MongoClient('localhost', 27017)
db = client[dbname]

contacts = db.contacts
contacts.drop()
os.system('mongoimport -d ' + dbname + ' -c contacts adressen.json')<jupyter_output>Mongo version 3.11.0<jupyter_text>Toevoegen van een "vreemd" document<jupyter_code>contacts.insert_one({"kleur": "groen", "prijs": 400, "beschrijving": "fiets"})<jupyter_output><empty_output><jupyter_text>Zoeken op *type* van een veld:<jupyter_code>list(contacts.find({"kleur": {"$type": "string"}}))<jupyter_output><empty_output><jupyter_text>MongoDB collection-validator (schema)* MongoDB query met namen en types* met de mogelijkheid van alternatieven* *minimale eis* aan documenten in collection (overige velden vrij)<jupyter_code>contact_schema = {"name": {"$type": "string"},
                  "$or": [{"email": {"$type": "string"}},
                          {"tel": {"$type": "string"}}

                         ]}
list(contacts.find(contact_schema))<jupyter_output><empty_output><jupyter_text>Vinden van documenten die *niet* aan schema voldoen:<jupyter_code>list(contacts.find({"$nor": [contact_schema]}))<jupyter_output><empty_output><jupyter_text>Koppelen van schema (validator) aan collection:<jupyter_code>db.command("collMod", "contacts", validator=contact_schema)<jupyter_output><empty_output><jupyter_text>toevoegen van niet-valide document:<jupyter_code>contacts.insert_one({"naam": "<NAME>", "email": "<EMAIL>"})<jupyter_output><empty_output><jupyter_text>Dat is handiger op te lossen met exception handling<jupyter_code>try:
    contacts.insert_one({"name": "<NAME>", "email": "<EMAIL>"})
except pymongo.errors.WriteError as s:
    print("Document not inserted: " + str(s))
else:
    print("insert OK")<jupyter_output>insert OK<jupyter_text>toevoegen van valide document:<jupyter_code>try:
    contacts.insert_one({"name": "<NAME>", "email": "<EMAIL>"})
except pymongo.errors.WriteError as s:
    print("Document not inserted: " + str(s))
else:
    print("insert OK")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1441631504922644, 'fraction_numerical': 0.005977496483825597, 'mean_word_length': 6.859116022099448, 'pattern_counts': {'":': 20, '<': 44, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '370646', 'license': 'MIT', 'n_tokens_mistral': 1014, 'n_tokens_neox': 990, 'n_words': 210, 'path': 'presentaties/schemas-presentatie.ipynb', 'repo_name': 'eelcodijkstra/mongodb-binder', 'size': '2892'}