猫鼬模式可选字段 [英] Mongoose schema optional fields

查看:64
本文介绍了猫鼬模式可选字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在nodejs中有一个猫鼬的用户架构

I have a user schema with mongoose in nodejs like this

userschema = mongoose.Schema({
    org: String,
    username: String,
    fullname: String,
    password: String,
    email: String
});

除了有时我需要添加一些其他字段.

Except sometimes I need to add some more fields.

主要问题是:我可以在猫鼬模式中具有可选字段吗?

推荐答案

猫鼬模式中的所有字段默认情况下都是可选的(当然,_id除外).

All fields in a mongoose schema are optional by default (besides _id, of course).

仅当您在其定义中添加 required: true 时,才需要该字段.

A field is only required if you add required: true to its definition.

因此,将您的架构定义为所有可能字段的超集,并在必填字段中添加required: true.

So define your schema as the superset of all possible fields, adding required: true to the fields that are required.

这篇关于猫鼬模式可选字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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