【问题标题】:Validation of referenced value coming from client-side app验证来自客户端应用程序的引用值
【发布时间】:2017-11-19 06:58:49
【问题描述】:

我正在使用 mongodb 并试图找到一种解决方案来预先创建数据供用户选择,并且每个界面只使用一个查询(一个用于用户的选择界面,另一个用于显示选定的数据)。我想出的是创建两个集合:一个用于可以选择的数据,另一个用于存储选定的数据。让我们以国家和城市为例。我有一个国家集合,就像:

{
    name: { type: String },
    cities: [{
        name: { type: String }
    }]
}

将使用此集合输入国家和城市数据。当用户创建配置文件时,他/她将能够使用选择框仅选择这些值。然后它将被保存在另一个集合中:

{
    name: { type: String },
    surname: { type: String },
    addresses: [{
        country: { type: String },
        city: { type: String },
        description { type: String }
    }]
}

现在想到的问题是如何确保恶意用户无法发送不同的国家或城市名称并使数据不一致。我希望能够使用用户的地址数据更新国家和城市名称。而且我也想知道这是一种正确的方式还是一种反模式?

【问题讨论】:

    标签: database-design mongoose mongoose-schema


    【解决方案1】:

    使用枚举

    var cities = ['LA', 'New York', 'Washington', 'Orlando']
    
    var s = new Schema({
        name: { type: String },
        cities: [{
            name: { type: String, enum: cities }
        }]
    })
    
    
    var personSchema = {
        name: { type: String },
        surname: { type: String },
        addresses: [{
            country: { type: String },
            city: { type: String, enum: cities },
            description { type: String }
        }]
    }
    

    Check mongoose enum documentation

    【讨论】:

    • 我知道枚举,但这不是一个解决方案。如果超级用户想要添加一个带有一组城市的新国家怎么办?它应该存储在数据库中吗?但是怎么做?正确的做法是什么? @SantiagoQuinteros
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-08
    • 1970-01-01
    • 1970-01-01
    • 2015-11-03
    相关资源
    最近更新 更多