【发布时间】:2020-06-16 04:35:20
【问题描述】:
我正在使用 $jsonSchema 进行 Mongo DB (4.2) 模式验证。我想使用 $jsonSchema 定义外键。是否可以? 这是我想要的一个例子。
收集客户 -> { customerid, customerName, customerAddress }
收款订单 -> { orderId, customerid, orderdate, orderdetail }
db.createCollection("order", {
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "orderId", "customerId", "orderdate", "orderdetail" ],
properties: {
orderId: {
bsonType: "string",
description: "must be a string and is required"
},
customerId: {
bsonType: "string",
description: "must be a string and is required"
**can i define some link here that this customer ID should come from customer collection**
},
orderdate: {
bsonType: "string",
description: "must be a string and is required"
},
orderdetail: {
bsonType: "string",
description: "must be a string and is required"
}
}
}
}
})
我想用 $jsonSchema 实现以下场景: 1)当我在订单集合中插入时,如果该客户集合中的客户不存在,则订单插入应该失败。 2)如果客户被删除,所有与其对应的订单也应该被删除。
有可能吗?
【问题讨论】:
-
我不相信您可以使用 JSON Schema 检查引用完整性。 (此外,无论如何,mongodb 并不是真的设计为这样做的。-mongodb.com/blog/post/thinking-documents-part-2)
标签: mongodb jsonschema json-schema-validator