假设我有一个包含以下文件的“城市”集合:
文件 1:
{
"_id": {
"$oid": "5e00979d7c21388869c2c048"
},
"cityName": "New York"
}
文件 2:
{
"_id": {
"$oid": "5e00979d7c21388869c2c432"
},
"cityName": "Los Angeles"
}
我想用以下文件创建另一个集合“学生”:
{
"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。