有动态值时如何使用猫鼬模式? [英] How to use mongoose schema when you have dynamic values?

查看:100
本文介绍了有动态值时如何使用猫鼬模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建架构,其中body可以基于传入事件在其中包含不同的键.因此,当我尝试呈现数据时,仅将_id发送到客户端event并不是结果的一部分.我是否为此方法实施了错误的架构?

I am trying to create schema where body can have different keys in it based on the incoming event. So when i try to rendered data it just send _id to client event is not part of results. Did i implemented wrong schema with for this approach ?

event.model.js

event.model.js

var mongoose = require('bluebird').promisifyAll(require('mongoose'));


var bmpEventSchema = new mongoose.Schema({
  event: {
    type: String,
    body : {}
  }
});

export default mongoose.model('BmpEvent', bmpEventSchema);

JsonDocument

JsonDocument

{
    "_id" : ObjectId("596f672f4c387baa25db5ec6"),
    "event" : {
        "type" : "json",
        "body" : {
            "evntType" : "Smtduki",
            "tkt" : "75522655",
            "cat" : "RNT",
            "esc_lvl" : "4",
            "asset" : "DNEC843027 ATI",
            "esc_tm" : "2017-05-26 09:18:00",
            "tos" : "T3APLS",
            "mcn" : "SL6516",
            "cusTkt" : "",
            "tktSrc" : "BMP",
            "tier1" : "STLMOETS"
        }
    }
}

推荐答案

这是判别器的用例.您可以将body设置为Mixed类型,但是这会违背猫鼬提供验证的目的.假设您正在对书籍的数据库进行建模.您将一个名为教授"的密钥用于学术书籍.但是,然后您需要为小说创作一位重要的小说家.您需要存储小说的类型,而不是教育书籍的类型. 现在,您可以像在用例中那样键入类型键,并处理结果.但随后,您可能必须为小说中的小说家应用默认值.或者,您可能需要设置其中一种类型而不是另一种类型的必填字段.这种方法的另一个问题是对不同类型使用中间件(挂钩).您可能想在小说创作上执行不同的功能,而在教育书创作上执行不同的功能.这只是一种情况,您可能有10或15种类型,处理起来会更加麻烦. 现在,为了避免这些问题,您可以为每种类型创建不同的模型.但是,如果这样做,则当您要查询所有书籍时,您将不得不对效率低下的每个模型执行查询.您在ODM层上需要一些东西.这就是歧视因素发挥作用的地方. 您可以在所有类型的书中使用所需的所有键来创建基本模型,并向其添加判别器键(请参阅文档).然后,您可以使用该模型的区分函数创建小说,并添加仅在小说中可用的其他键.您可以按照这种方式创建任意数量的子模型,然后可以简单地以多态方式使用它们.在内部,这将创建一个名为"books"的单一集合,但对于小说,它将仅存储小说的密钥. ODM层将处理不同类型模型的验证,中间件等. http://mongoosejs.com/docs/discriminators.html

This is a use case for discrimnators. You can make body a Mixed type but that will defeat the purpose of mongoose to provide validation. Suppose you have are modeling a books' database. You make a key named Professor for an academic book. But then you need to make a key novelist for a novel. You need to store genre for novel but not for educational books. Now you can make a type key like you did in your use case and play with the results. But then you may have to apply default values for novelist in novels. Or you may need to set a field required in one of the types and not the other. Another problem with that approach would be to use middlewares (hooks) to the different types. You may want to perform a different function on creation of novel and a different function on creation of an educational book. It is just a scenario and you can have like 10 or 15 types which will be even more cumbersome to handle. Now in order to avoid these issues you can make a different model for each type. But if you do that, when you want to query all books, you will have to perform a query on each of the models which will be ineffecient. You need something on the ODM layer. This is where discriminators come into play. You make a base model with all the keys you want in all types of books and add a discrimnator key to it(refer to docs). Then you create novel from this model's discriminator function and add additional keys which will only be in novel. You can create as many child models as you like this way and then you can use them in simply a polymorphic manner. Internally, this will create a single collection named books but for novels it will store only novels' keys. The validation, middlewares etc of the different types of models will be handled by the ODM layer. http://mongoosejs.com/docs/discriminators.html

这篇关于有动态值时如何使用猫鼬模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆