为具有异构数组的对象创建猫鼬模式

时间:2019-04-24 14:30:08

标签: node.js mongodb mongoose

给出对象:

let defaultProfile = {
      codecs: [
        {
          odatatype: '#Microsoft.Media.H264Video',
          keyFrameInterval: 'PT2S',
          layers: [
            {
              odatatype: '#Microsoft.Media.H264Layer',
              profile: 'Main',
              level: '3.1',
              bufferWindow: 'PT5S',
              bFrames: 3,
              referenceFrames: 3,
              adaptiveBFrame: true,
              frameRate: '24000/1001',
              bitrate: 2940000,
              maxBitrate: 2940000,
              width: '1280',
              height: '720',
              label: '1280x720'
            },
            {
              odatatype: '#Microsoft.Media.H264Layer',
              profile: 'Main',
              level: '3.1',
              bufferWindow: 'PT5S',
              bFrames: 3,
              referenceFrames: 3,
              adaptiveBFrame: true,
              frameRate: '24000/1001',
              bitrate: 320000,
              maxBitrate: 320000,
              width: '320',
              height: '180',
              label: '320x180'
            }
          ]
        },
        {
          odatatype: '#Microsoft.Media.AacAudio',
          channels: 2,
          samplingRate: 48000,
          bitrate: 128000,
          profile: 'AacLc',
          label: 'aacAudio'
        }
      ]
    }

其中包含一个异构数组,是否有任何合法方法为其创建猫鼬模式?

我尝试过

let amsProfileSchema =   {
      id: { type: String },
      codecs: [
        {
          odatatype: { type: String, enum: ['#Microsoft.Media.H264Video'] }, 
          keyFrameInterval: { type: String },
          layers: [
            {
              // https://docs.microsoft.com/de-de/rest/api/media/transforms/createorupdate#h264layer
              odatatype: { type: String, enum: ['#Microsoft.Media.H264Layer'] }, 
              profile: { type: String, enum: ['Baseline', 'Main', 'High', 'High422', 'High444'] },
              level: { type: String },
              bufferWindow: { type: String },
              bFrames: { type: Number },
              referenceFrames: { type: Number },
              adaptiveBFrame: { type: Boolean },
              frameRate: { type: String },
              bitrate: { type: Number, required: true },
              maxBitrate: { type: Number },
              width: { type: String },
              height: { type: String },
              label: { type: String }
            }
          ]
        },
        {
          // https://docs.microsoft.com/de-de/rest/api/media/transforms/createorupdate#aacaudio
          odatatype: { type: String, enum: ['#Microsoft.Media.AacAudio'] },
          channels: { type: Number },
          samplingRate: { type: Number },
          bitrate: { type: Number },
          profile: { type: String, enum: ['AacLc', 'HeAacV1', 'HeAacV2'] },
          label: { type: String }
        }
      ]
    }

但是当我尝试像这样使用它时:

    const mongooseSchema = mongoose.Schema(Object.assign({}, amsProfileSchema))

    let AMSProfileModel = mongoose.model('ams', mongooseSchema)
    let amsProfileInstance = new AMSProfileModel(defaultProfile)

尽管第一个编解码器数组条目有效,但第二个则无效。当amsProfileInstance出现时:

{
  _id: '5cc06f897b2e490011a71c00',
  codecs: [
    {
      _id: '5cc06f897b2e490011a71c02',
      odatatype: '#Microsoft.Media.H264Video',
      keyFrameInterval: 'PT2S',
      layers: [
        {
          _id: '5cc06f897b2e490011a71c07',
          odatatype: '#Microsoft.Media.H264Layer',
          profile: 'Main',
          level: '3.1',
          bufferWindow: 'PT5S',
          bFrames: 3,
          referenceFrames: 3,
          adaptiveBFrame: true,
          frameRate: '24000/1001',
          bitrate: 2940000,
          maxBitrate: 2940000,
          width: '1280',
          height: '720',
          label: '1280x720'
        },           
        {
          _id: '5cc06f897b2e490011a71c03',
          odatatype: '#Microsoft.Media.H264Layer',
          profile: 'Main',
          level: '3.1',
          bufferWindow: 'PT5S',
          bFrames: 3,
          referenceFrames: 3,
          adaptiveBFrame: true,
          frameRate: '24000/1001',
          bitrate: 320000,
          maxBitrate: 320000,
          width: '320',
          height: '180',
          label: '320x180'
        }
      ]
    },
    { _id: '5cc06f897b2e490011a71c01', 
      odatatype: '#Microsoft.Media.AacAudio', 
      layers: [] 
    }
  ]
}

看起来(也许)正在将第二个ray条目强制为第一个格式?

还是我犯了我看不见的其他愚蠢错误?

或者我的整体方法是错误的-如果是这样,正确的方法是什么? (我想将这些对象保存到MongoDb中)

3 个答案:

答案 0 :(得分:1)

您可能正在寻找混合模式类型。这是关于它的文档页面。

https://mongoosejs.com/docs/schematypes.html#mixed

答案 1 :(得分:1)

据我了解,Mongoose Schema不支持这种性质的数组。您只能定义一种项目类型。

此结构在您插入的所有文档中是否一致?即长度为2的编解码器,第一个有层,第二个没有层?如果是这样,您可以将架构重新安排到单独的字段中,即分层和非分层编解码器。每个人都可以拥有与所示匹配的自己的模式。然后可以将传入的对象强制地强制为这种格式。

否则,您只能定义一种编解码器元素类型,并为一个元素提供所有可能的字段。

答案 2 :(得分:1)

我可以建议您选择

严格:错误

严格:True(默认)在做什么?

strict选项(默认情况下启用)可确保传递给我们的模型构造函数的未在架构中指定的值不会保存到数据库中。

https://mongoosejs.com/docs/guide.html#strict

相关问题