首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在猫鼬NodeJs中保存4级嵌套模式的最佳方法是什么?

在猫鼬NodeJs中保存4级嵌套模式的最佳方法是什么?
EN

Stack Overflow用户
提问于 2022-03-14 10:48:44
回答 1查看 56关注 0票数 0

我有4级嵌套模式:

框架中有域引用,域有引用它的控件,control有引用它的SubControl。

现在,我已经寻找了一段时间,我一直感到困惑。

第一个问题:可以发布来自它自身的框架的所有数据吗?第二个问题:我使用了引用ID的方法,我应该改用subDocuments吗?

框架模式:

代码语言:javascript
复制
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);

域架构:

代码语言:javascript
复制
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);

我的控制模式:

代码语言:javascript
复制
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);

我的SubControl模式

代码语言:javascript
复制
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);

现在,我试图从framework中发布这些嵌套文档:

代码语言:javascript
复制
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中的所有数据都发布出来?

试图从邮递员那里发这篇文章:

代码语言:javascript
复制
{
 "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"
    }
  }
  ]
}

只有域、控件和subControl的id的保存在mongodb中,这就是它的工作方式吗?在这个例子中,框架是一个模型中的所有数据吗?或者我应该使用嵌入式方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-15 05:35:21

在有大量引用的场景中,我将做什么(顺便说一句,猫鼬将它命名为ref,它允许您填充)。

带有域引用的frameWork架构的示例。

代码语言:javascript
复制
const frameworkSchema = mongoose.Schema({
    domains: [{type: mongoose.Schema.Types.ObjectId, ref: 'Domain'}],
})
const FrameworkModel = mongoose.model('Framework', frameworkSchema)

上面的Domain指的是一个域模型。我们现在可以创建一个域模型了。

代码语言:javascript
复制
const domainSchema = mongoose.Schema({
    _id: { type: mongoose.Schema.Types.ObjectId } //this is the default
})
const DomainModel = mongoose.model('Domain', domainSchema);

示例使用-我们希望获取与特定架构相关的所有域信息。

代码语言:javascript
复制
const results = FrameworkModel.findOne({ _id: 'some framework id'}).populate('domains').lean({ virtuals: true });

结果会是

代码语言:javascript
复制
{ 
    _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来维护,以便轻松地引用它们。这是一个比在一个文档中嵌套多个级别更好的设计。

除非有必要,否则使用单独的集合比使用子文档有更多的好处。(更容易找到文档,更容易更新文档,当然,更有表现力)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71466512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档