Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 创建mongodb集合时,如何在架构验证中添加数据库引用?_Python_Mongodb_Pymongo_Dbref - Fatal编程技术网

Python 创建mongodb集合时,如何在架构验证中添加数据库引用?

Python 创建mongodb集合时,如何在架构验证中添加数据库引用?,python,mongodb,pymongo,dbref,Python,Mongodb,Pymongo,Dbref,假设我有一个包含以下文档的“城市”集合: 文件1: { "_id": { "$oid": "5e00979d7c21388869c2c048" }, "cityName": "New York" } 文件2: { "_id": { "$oid": "5e00979d7c21388869c2c432" }, "cityName": "Los Angeles" } 我想用以下文档创建另一个集合“students”:

假设我有一个包含以下文档的“城市”集合:

文件1:

{
    "_id": {
        "$oid": "5e00979d7c21388869c2c048"
    },
    "cityName": "New York"
}
文件2:

{
    "_id": {
        "$oid": "5e00979d7c21388869c2c432"
    },
    "cityName": "Los Angeles"
}
我想用以下文档创建另一个集合“students”:

{
    "name": "John",
    "citiesVisited": [
        {
            "$ref": "cities",
            "$id": "5e00979d7c21388869c2c048"
        },
        {
            "$ref": "cities",
            "$id": "5e00979d7c21388869c2c432"
        }
    ]
}
模式验证应该如何进行?我尝试了以下验证:

validator = {
    "$jsonSchema": {
        "bsonType": "object",
        "required": ["name", "citiesVisited"],
        "properties": {
            "name": {
                    "bsonType": "string",
                    "description": "name of student."
            },
            "citiesVisited": {
                "bsonType": ["array"],
                "items": {
                    "bsonType": "object",
                    "required": ["$ref", "$id"],
                    "properties": {
                        "$ref": {
                                "bsonType": "string",
                            "description": "collection name"
                        },
                        "$id": {
                            "bsonType": "string",
                            "description": "document id of visited city"
                        }
                    }
                },
                "description": "cities visited by the student"
            }
        }
    }}
但是,当我尝试获取数据库中所有集合的列表时,会出现以下错误:

bson.errors.InvalidBSON: collection must be an instance of str
我尝试在没有“$ref”和“$id”中的“$”的情况下创建验证,它工作正常,但由于数据库引用,文档验证失败

我想在存储城市时使用dbrefs