MongoDB引用模式设计的问题



我想定义一个MongoDB用户模式。

如果我在设置验证的地方创建了一个User集合,那么我想在架构中指定organizationId字段引用组织集合中的Organisation对象。

我该如何实现这一目标?

我知道Mongo不应该以这种方式使用,但在这个时候,这是至关重要的。使用猫鼬不是一种选择。

应为->

db.createCollection('users') 
// Where users has set validation organisationId: Object(Organisation)

感谢您的支持。

您可以为此使用验证器!例如:

db.createCollection("students", {
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "name", "year", "major", "address" ],
properties: {
name: {
bsonType: "string",
description: "must be a string and is required"
},
year: {
bsonType: "int",
minimum: 2017,
maximum: 3017,
description: "must be an integer in [ 2017, 3017 ] and is required"
},
major: {
enum: [ "Math", "English", "Computer Science", "History", null ],
description: "can only be one of the enum values and is required"
},
gpa: {
bsonType: [ "double" ],
description: "must be a double if the field exists"
},
address: {
bsonType: "object",
required: [ "city" ],
properties: {
street: {
bsonType: "string",
description: "must be a string if the field exists"
},
city: {
bsonType: "string",
"description": "must be a string and is required"
}
}
}
}
}
}
})

最新更新