Arrays of schema types on Mongoose

被刻印的时光 ゝ 提交于 2021-02-07 08:20:37

问题


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. Mongoose doesn't seem to validate/use the schema type when in an array - works fine without the array.

How can I define an array of schema types that will validate?


回答1:


Answer from Mongoose creator:

"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"

I ended up creating subdocuments as Mongoose supports validation on them when in array form.

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

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



回答2:


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



来源:https://stackoverflow.com/questions/15047520/arrays-of-schema-types-on-mongoose

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!