简体   繁体   中英

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. 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?

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]
  }
});

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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