简体   繁体   中英

Python MongoDB JSON schema validator “unresolved reference”

I am relatively new to both Python and MongoDB, I am using python to set up a MongoDB database and create a collection with schema validation. However, when I create the collection as specified in the MongoDB documentation like so:

db.create_collection("collection", {
        validator: {
            "$schema": "schema_stuff",
            "property1":"..."
        }

Pycharm throws an error saying: "Unresolved reference 'validator' "

I suspect it may have something to do with my import but I'm not sure.

from pymongo import MongoClient

Any ideas why this might be happening?

It happens because what you try to put into validator is a dictionary and validator is supposed to be a variable/object which is not known to Python. correct is to have 'validator' or "validator" instead of validator .

Correct query to create collection with validator should be like this:

validator = {'validator': {'your validation stuff'}}    
db.create_collection("collection", validator=validator)

Or like this:

db.create_collection("collection", validator={'validator': 'your validation stuff'})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM