我有 4 級嵌套模式:
框架參考了域,域參考了控制元件,控制元件參考了子控制元件。
現在我一直在尋找一段時間,我一直感到困惑。
第一個問題:是否可以從它自己的框架中發布所有資料? 第二個問題:我使用了 ID 參考方法,我應該切換到使用 subDocuments 嗎?
框架架構:
const FrameworkSchema = new Schema({
name: {
type: String,
trim: true
},
description: {
type: String,
trim: true
},
domain: [{
domain: {type: Mongoose.Schema.Types.ObjectId, ref: 'Domain'}
}],
updated: Date,
created: {
type: Date,
default: Date.now
}
});
module.exports = Mongoose.model('Framework', FrameworkSchema);
域架構:
const DomainSchema = new Schema({
_id: {
type: Schema.ObjectId,
auto: true
},
domainNo: {
type: String,
trim: true
},
domainName: {
type: String,
trim: true
},
domainDescription: {
type: String,
trim: true
},
framework: {
type: Mongoose.Schema.Types.ObjectId,
ref: 'Framework'
},
control: [{
control: {type: Mongoose.Schema.Types.ObjectId, ref: 'Control'}
}],
updated: Date,
created: {
type: Date,
default: Date.now
}
});
module.exports = Mongoose.model('Domain', DomainSchema);
我的控制架構:
const ControlSchema = new Schema({
_id: {
type: Schema.ObjectId,
auto: true
},
mainControl: {
type: String
},
subControl: [{
subControlNo: {type: Mongoose.Schema.Types.String, ref: 'SubControl'}
}],
controlDescription: {
type: String,
trim: true
},
updated: Date,
created: {
type: Date,
default: Date.now
}
});
module.exports = Mongoose.model('Control', ControlSchema);
我的子控制元件架構
const SubControlSchema = new Schema({
_id: {
type: Schema.ObjectId,
auto: true
},
subControlNo: {
type: [String]
},
updated: Date,
created: {
type: Date,
default: Date.now
}
});
module.exports = Mongoose.model('SubControl', SubControlSchema);
現在我正在嘗試從框架 api 發布這個嵌套檔案:
router.post(
'/add',
auth,
role.checkRole(role.ROLES.Admin), async (req, res) => {
try {
const subControl = new SubControl({...req.body});
const subControlDoc = await subControl.save();
const control = new Control({...req.body}); // take the control data
control.subControl.push(subControlDoc._id); // take the subControl and push the ID into the control
const controlDoc = await control.save();
//make the subcontrol pushed into control
// make control pushed in domain
const domain = new Domain({...req.body});
domain.control.push(controlDoc._id);
const domainDoc = await domain.save();
const framework = new Framework({...req.body});
framework.domain.push(domainDoc._id);
const frameworkDoc = await framework.save(); //save the framework domain's ID
res.status(200).json({
success: true,
message: `Framework has been added successfully!`,
framework: frameworkDoc
});
} catch (error) {
res.status(400).json({
error
// error: 'Your request could not be processed. Please try again.'
});
}
}
);
現在我使用 push 將資料作為陣列推送,不確定這是否正確,是否可以從框架 api 發布所有資料?
試圖從郵遞員那里發布這個:
{
"name": "ISO780001",
"description": "frameworkDescription",
"domain":
[
{
"domainNo": "11",
"domainName": "domainName00",
"domainDescription": "domaindescu0",
"control": [{
"mainControl": "1-4",
"subControl": [{
"subControlNo": "1-4-1"
},
{
"subControlNo": "1-4-2"
}],
"controlDescription": "controlDescriptionTest"
},
{
"mainControl": "1-4",
"subControl": [{
"subControlNo": "1-4-1"
},
{
"subControlNo": "1-4-2"
}],
"controlDescription": "controlDescriptionTest"
}
]
},
{
"domainNo": "1-2",
"name": "domainName00",
"description": "domaindescu0",
"control": {
"mainControl": "1-4",
"subControl": [{
"subControlNo": "1-4-1"
},
{
"subControlNo": "1-4-2"
}],
"controlDescription": "controlDescriptionTest"
}
}
]
}
只有域、控制元件和子控制元件的 id 保存在 mongodb 中,這是它的作業原理嗎?在這種情況下,我可以從一個模型發布所有資料嗎?還是我應該使用嵌入式方法?
uj5u.com熱心網友回復:
在我有很多參考的情況下我會做什么(ref順便說一下貓鼬的名字,它允許你填充)。
具有域參考的框架模式示例。
const frameworkSchema = mongoose.Schema({
domains: [{type: mongoose.Schema.Types.ObjectId, ref: 'Domain'}],
})
const FrameworkModel = mongoose.model('Framework', frameworkSchema)
Domain以上是指域模型。我們現在可以創建一個域模型。
const domainSchema = mongoose.Schema({
_id: { type: mongoose.Schema.Types.ObjectId } //this is the default
})
const DomainModel = mongoose.model('Domain', domainSchema);
示例用法 - 我們想要獲取與特定模式相關的所有域資訊。
const results = FrameworkModel.findOne({ _id: 'some framework id'}).populate('domains').lean({ virtuals: true });
結果將類似于
{
_id: 'some framework id',
name: 'name of framework',
domains: [
{
_id: 'id of domain 1',
name: 'example.com'
},
{
_id: 'id of domain 2',
name: 'example2.com'
}
]
}
您還可以探索虛擬物件,了解如何將框架、域和其他控制元件保持為獨立collections的,以便輕松參考它們。這是一個比在單個檔案中嵌套多個級別更好的設計。
除非有必要,否則使用單獨的集合比使用子檔案更有好處。(更容易找到檔案,更容易更新檔案,當然也更高效)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/443988.html
標籤:节点.js mongodb 猫鼬 猫鼬模式 nodejs-服务器
