Mongoose 上的模式类型数组 [英] Arrays of schema types on Mongoose

查看:35
本文介绍了Mongoose 上的模式类型数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个架构:

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

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

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?

推荐答案

Mongoose creator 的回答:

Answer from Mongoose creator:

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

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

我最终创建了子文档,因为 Mongoose 支持在数组形式时对它们进行验证.

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

这篇关于Mongoose 上的模式类型数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆