简体   繁体   English

Mongoose上的模式类型数组

[英]Arrays of schema types on Mongoose

I have a schema: 我有一个架构:

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

In this case I am using the url schema type from https://github.com/bnoguchi/mongoose-types - but I have tried this with other types. 在这种情况下,我使用来自https://github.com/bnoguchi/mongoose-types的url架构类型 - 但我已尝试使用其他类型。 Mongoose doesn't seem to validate/use the schema type when in an array - works fine without the array. Mongoose似乎没有在数组中验证/使用模式类型 - 没有数组工作正常。

How can I define an array of schema types that will validate? 如何定义将验证的模式类型数组?

Answer from Mongoose creator: 来自Mongoose创作者的回答:

"Unless the Url is a subdocument, validation will not get triggered currently (there is a ticket open somewhere to support richer types). The work-around is to define validation on the array: https://gist.github.com/aheckmann/12f9ad103e0378db6afc " “除非Url是一个子文档,否则当前不会触发验证(在某个地方打开一个票据以支持更丰富的类型)。解决方法是在阵列上定义验证: https//gist.github.com/aheckmann / 12f9ad103e0378db6afc

I ended up creating subdocuments as Mongoose supports validation on them when in array form. 我最终创建了子文档,因为Mongoose在数组形式时支持对它们进行验证。

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM