node.js - 如何处理 Mongoose 中的 ENUM 值?

标签 node.js mongodb mongoose enums schema

背景

我有一个 Mongoose 模式,它定义了给定对象可以具有的一组可能值。

const mongoose = require("mongoose");

const COUNTRIES = ["ES", "PT", "US", "FR", "UK"];
const GENDERS = ["M", "F"];

const surveySchema = {
    subject: { type: String, required: true },
    country: { type: String, enum: COUNTRIES },
    target: {
        gender: { type: String, enum: GENDERS }
    }
};

module.exports = new mongoose.Schema(surveySchema);;
module.exports.modSchema = surveySchema;

为什么我不喜欢 ENUM
我个人不喜欢ENUM值,因为如果我向 ENUM 添加另一个值,我必须重新编译整个应用程序并进行部署。

我猜想用 ENUM比如性别,那永远不会改变,这不是问题。

但是,对于国家/地区,我的 SQL 方面告诉我应该存储它们,因为如果您的业务不断增长,您可能会扩展到其他国家/地区。

问题

我的问题是我不知道如何在架构级别告诉 Mongoose,国家/地区的唯一允许值必须是 ["ES", "PT", "US", "FR", "UK"] .

我想我可以创建一个收集国家,但我缺乏如何连接它们的知识。我必须使用异步验证器吗?

您将如何处理 ENUM那可以改变吗?

最佳答案

您可以使用管理面板将更多国家/地区添加到国家/地区集合中。正如您所说的 COUNTRIES 数组可以增长,您可以使用另一个集合从管理面板按需添加更多国家/地区。

当您要在调查中添加/保存新记录时,您可以触发预保存 Hook 到 mongo 进行验证。

假设我们对这样的国家有另一种模式。

{
 countries: [String]
}

这是该场景的示例代码。
const mongoose = require("mongoose");

const GENDERS = ["M", "F"];

const surveySchema = {
    subject: { type: String, required: true },
    country: { type: String},
    target: {
        gender: { type: String, enum: GENDERS }
    }
};

var Survey = new mongoose.Schema(surveySchema);

Survey.pre('save',function(next){
  var me = this;
  CountryModel.find({},(err,docs)=>{
    (docs.countries.indexOf(me.country) >=0) ? next() : next(new Error('validation failed'));
  });
});

这样您就可以处理动态国家添加,而无需更改国家/地区数组并重新部署您的整个服务器。

使用自定义验证器
const mongoose = require("mongoose");

const GENDERS = ["M", "F"];

const surveySchema = {
        subject: {
            type: String,
            required: true
        },
        country: {
            type: String,
            validate: {
                isAsync: true,
                validator: function(arg, cb) {
                    CountryModel.find({}, (err, docs) => {
                                if (err) {
                                    cb(err);
                                } else {
                                    cb(docs.countries.indexOf(arg) >= 0);
                                }
                            }
                        },
                        message: '{VALUE} is not a valid country'
                }
            },
            target: {
                gender: { type: String, enum: GENDERS }
            }
        };

在回调中保存调查数据时会出现错误..
ServeyModel.save((err,doc)=>{
if(err){
console.log(err.errors.country.message);
//Error handle
}else {
//TODO
}
});

关于node.js - 如何处理 Mongoose 中的 ENUM 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43159336/

相关文章:

node.js - 如何检查nodejs函数的结果?

json - sequelize.query() 两次返回相同的结果

node.js - 将 Mongoose 模型包含到包含在 node.js 文件中的单个 js 中?

javascript - 用于快速路由的正确正则表达式语法是什么?

java - Spring Data MongoDB - 忽略空对象

node.js - 如何在nodeJS和mongoDB中进行聚合

node.js - Mongoose 复合索引唯一 + 稀疏

javascript - Mongoose.js : Find users, 然后检查用户的值

node.js - Mongoose 模型 update() 与 save()

node.js - 删除 Mongoose 中的多对多引用