猫鼬模式 - 嵌套 objects 具有共享属性
Mongoose schema - nested objects with shared properties
我是 MongoDB 和 Mongoose 的新手,正在尝试找出处理创建我的模式的最佳方法。我更习惯于关系数据库,但我希望它能与 Mongo.
一起使用
我正在创建“扩展”其他 object 的 object。例如,我有一个人 object,我想将其用作 parent 或盛大 parent object 的起点。 parent 和 grandparent 都可能具有超出基本人所具有的附加值(我只是在每个中包含一个示例)...
const personSchema = new mongoose.Schema({
name: String,
birthDate: Date,
deathDate: Date,
});
const parentSchema = new mongoose.Schema({
//all the stuff a person has + below:
children: [personSchemas?] //[array of persons, important... parents can also be children,
multiple parents will share the same child]
parentingStyle: String,
})
const grandParentSchema = new mongoose.Schema({
// stuff that a parent has plus
grandparentingStyle: String,
})
我认为以下 post 包含一些可能对您有所帮助的答案:
Referencing another schema in Mongoose
除此之外,我可能还建议您将 parents 和 grandparents 定义为这样的角色:
var mongoose = require('mongoose');
const PersonSchema = new mongoose.Schema({
person_id: {
type: mongoose.Schema.Types.ObjectId
},
name: String,
birthDate: Date,
deathDate: Date,
//the following is if you want later to fetch persons by role
role: {
type: string,
enum: ["parent", "grandParent","child"],
}
});
//Then you could do it this way
const ParentSchema = new mongoose.Schema({
//all the stuff a person has + below:
children: [personSchemas],
parentingStyle: String
})
//Or this way
const ParentSchema = new mongoose.Schema({
//all the stuff a person has + below:
children: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Person'
}
parentingStyle: String,
})
const GrandParentSchema = new mongoose.Schema({
// same as for parents (ether way)
// stuff that a parent has plus
grandparentingStyle: String
})
在 non-relational 数据库中,这实际上取决于您以后想对数据做什么。 “关节”(与关系数据库相比)创建起来要简单得多,可以通过引用 id 或获取整个数组(就像您所做的那样),也可以通过稍后进行更详细的查询来简单地创建。
我是 MongoDB 和 Mongoose 的新手,正在尝试找出处理创建我的模式的最佳方法。我更习惯于关系数据库,但我希望它能与 Mongo.
一起使用我正在创建“扩展”其他 object 的 object。例如,我有一个人 object,我想将其用作 parent 或盛大 parent object 的起点。 parent 和 grandparent 都可能具有超出基本人所具有的附加值(我只是在每个中包含一个示例)...
const personSchema = new mongoose.Schema({
name: String,
birthDate: Date,
deathDate: Date,
});
const parentSchema = new mongoose.Schema({
//all the stuff a person has + below:
children: [personSchemas?] //[array of persons, important... parents can also be children,
multiple parents will share the same child]
parentingStyle: String,
})
const grandParentSchema = new mongoose.Schema({
// stuff that a parent has plus
grandparentingStyle: String,
})
我认为以下 post 包含一些可能对您有所帮助的答案: Referencing another schema in Mongoose
除此之外,我可能还建议您将 parents 和 grandparents 定义为这样的角色:
var mongoose = require('mongoose');
const PersonSchema = new mongoose.Schema({
person_id: {
type: mongoose.Schema.Types.ObjectId
},
name: String,
birthDate: Date,
deathDate: Date,
//the following is if you want later to fetch persons by role
role: {
type: string,
enum: ["parent", "grandParent","child"],
}
});
//Then you could do it this way
const ParentSchema = new mongoose.Schema({
//all the stuff a person has + below:
children: [personSchemas],
parentingStyle: String
})
//Or this way
const ParentSchema = new mongoose.Schema({
//all the stuff a person has + below:
children: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Person'
}
parentingStyle: String,
})
const GrandParentSchema = new mongoose.Schema({
// same as for parents (ether way)
// stuff that a parent has plus
grandparentingStyle: String
})
在 non-relational 数据库中,这实际上取决于您以后想对数据做什么。 “关节”(与关系数据库相比)创建起来要简单得多,可以通过引用 id 或获取整个数组(就像您所做的那样),也可以通过稍后进行更详细的查询来简单地创建。