背景
我有一个 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/