存储未在架构中定义的值(动态架构?)

时间:2014-01-08 17:34:10

标签: mongodb mongoose

假设我有一个架构定义

var Users = mongoose.model('Users', new mongoose.Schema({
    username: String,
    salt: String,
    hash: String,
    facebook: {
        id: String
        }
    }));

但我想稍后

user = new Users({
    username: 'myusername',
    facebook: {
        id: '3141592653',
        displayName: 'mydisplayname'   // <- wasn't in the schema ^
    }
});

然后displayName根本没有存储。猫鼬不允许这样吗?因为我会想象,因为MongoDB是“无模式”的,所以应该有办法做到这一点吗?

1 个答案:

答案 0 :(得分:1)

您可以禁用架构上的strict选项,以允许保存架构中没有的字段:

 
var Users = mongoose.model('Users', new mongoose.Schema({
    username: String,
    salt: String,
    hash: String,
    facebook: {
        id: String
    }
}, { strict: false }));