【问题标题】:Arrays of schema types on MongooseMongoose 上的模式类型数组
【发布时间】:2013-02-09 10:46:00
【问题描述】:

我有一个架构:

var s = new Schema({
  links: {
    type: [Url]
  }
});

在这种情况下,我使用来自 https://github.com/bnoguchi/mongoose-types 的 url 模式类型 - 但我已经尝试过使用其他类型。 Mongoose 在数组中似乎没有验证/使用模式类型 - 没有数组也可以正常工作。

如何定义将验证的架构类型数组?

【问题讨论】:

  • 似乎它只适用于内置类型。看起来我不能在支票中注入新类型。

标签: javascript mongodb mongoose


【解决方案1】:

Mongoose 创作者的回答:

“除非 Url 是子文档,否则当前不会触发验证(在某处有一张票可以支持更丰富的类型)。解决方法是在数组上定义验证:https://gist.github.com/aheckmann/12f9ad103e0378db6afc

我最终创建了子文档,因为 Mongoose 支持以数组形式对其进行验证。

var links = new Schema({
  link: URL
});

var s = new Schema({
  links: {
   type: [links]
  }
});

【讨论】:

    【解决方案2】:

    试试var s = new Schema({links: [Url]});

    【讨论】:

      猜你喜欢
      • 2017-02-10
      • 2014-05-21
      • 1970-01-01
      • 2014-05-13
      • 2021-04-27
      • 2018-12-11
      • 2015-02-17
      • 2021-10-17
      • 1970-01-01
      相关资源
      最近更新 更多