Mongoose将strict设置为false会禁用验证

时间:2015-12-17 11:19:46

标签: node.js mongodb mongoose

我有以下架构

var Schema = new mongoose.Schema({
    type: {required: true, type: String, enum: ["device", "beacon"], index: true},
    device: {
        type: {type: String},
        version: {type: String},
        model: {type: String}
    },
    name: String,
    beaconId: {required: false, type: mongoose.Schema.Types.ObjectId},
    lastMeasuredTimestamp: {type: Number, index: true},
    lastMeasuredPosition: {type: [Number], index: "2dsphere"},
    lastMeasuredFloor: {type: Number, index: true}
}, {strict: false});

请注意,我已将strict设置为false。这是因为将未在架构中定义的自定义属性添加到文档中是有效的。

接下来,我执行以下查询 DB.Document.update({_id: "SOME_ID_HERE"}, {$set: {type: "bull"}}, {runValidators: true})

这将根据Mongoose架构将属性“type”更改为无效的值。我使用runValidators选项来确保运行模式验证。

然而,此查询的最终结果是'type'更改为'bull'并且未运行验证。当我将strict设置为true时,验证确实运行并且(正确)显示错误。

为什么严格影响验证是否运行?当我查看此描述http://mongoosejs.com/docs/guide.html#strict时,它只提到严格限制添加模式中未定义的属性(我不希望这个特定模式)。

安装信息:

  • Ubuntu 14.04 LTS
  • MongoDB 3.0.8
  • Mongoose 4.2.3
  • NodeJS 0.10.25
  • NPM 1.3.10

1 个答案:

答案 0 :(得分:4)

经过一些尝试后,我找到了一个有效的解决方案。如果任何未来的Mongoose用户遇到同样的问题,我会在这里发布。

诀窍是在Mongoose文档上使用save方法。出于某种原因,这确实可以正确运行验证器,同时还允许使用严格选项。

因此,更新文档的基本过程如下所示:

  • 使用Model.findOne
  • 查找文档
  • 将更新应用于文档(例如,通过合并现有文档中的更新值)
  • 在文档上调用save

在代码中:

    // Find the document you want to update
    Model.findOne({name: "Me"}, function(error, document) {
        if(document) {
            // Merge the document with the updates values
            merge(document, newValues);

            document.save(function(saveError) {
                // Whatever you want to do after the update
            });
        }
        else {
            // Mongoose error or document not found....
        }
    });

    // Merges to objects and writes the result to the destination object.
    function merge(destination, source) {
        for(var key in source) {
            var to = destination[key];
            var from = source[key];

            if(typeof(to) == "object" && typeof(from) == "object")
                deepMerge(to, from);
            else if(destination[key] == undefined && destination.set)
                destination.set(key, from);
            else
                destination[key] = from;
        }
    }