Mongoose upsert不会创建默认架构属性

str*_*ada 17 mongoose mongodb node.js

示例文档架构:

var CompanySchema = Schema({
    created: { type: Date, default: Date.now },
    modified: { type: Date, default: Date.now },
    address: { type: String, required:true },
    name: { type: String, required:true }
});
Run Code Online (Sandbox Code Playgroud)

我正在使用通用请求处理程序来编辑和创建"公司"文档:

exports.upsert = function(req, res) {
    helper.sanitizeObject(req.body);
    var company = {
        name: req.body.name,
        address: req.body.address
    };
    var id = req.body.id || new mongoose.Types.ObjectId();
    var queryOptions = {
        upsert: true
    };
    Company.findByIdAndUpdate(id, company, queryOptions).exec(function(error, result) {
        if(!error) {
            helper.respondWithData(req, res, {
                data: result.toJSON()
            });
        } else {
            helper.respondWithError(req, res, helper.getORMError(error));
        }
    });
};
Run Code Online (Sandbox Code Playgroud)

但是使用此方法时,插入新文档时created,modified不会使用默认值保存属性Date.now.现在我可以Company.create根据id的存在来调用,但我想知道为什么如果新文档中不存在属性,upsert不会使用默认值?

我使用的是Mongoose版~3.8.10,

Joh*_*yHK 35

发生的事情是,当调用任何"更新"系列方法时,没有使用Mongoose的验证,中间件或默认值findByIdAndUpdate.它们只能通过调用save或调用来调用create.

这样做的原因是"更新"调用实际上是传递给本机驱动程序,而Mongoose只根据模式定义提供字段的类型转换.

Mongoose 4.0更新

猫鼬的过程中创建新文档时现在支持设置的默认值update,findOneAndUpdatefindByIdAndUpdate更新插入.将setDefaultsOnInsert选项设置true为启用此选项.这使用$setOnInsert运算符在insert上创建默认值.

var queryOptions = {
    upsert: true,
    setDefaultsOnInsert: true
};
Company.findByIdAndUpdate(id, company, queryOptions).exec( ...
Run Code Online (Sandbox Code Playgroud)


Sar*_*man 8

你可以使用{ "$setOnInsert": { value: 31 } } Doc

参考:https://groups.google.com/forum/#!topic/ mongoose-orm/ WuJSqxPX8T8

语法:findOneAndUpdate([query], [doc], [options], [callback]) Doc

例:

model.findOneAndUpdate(
      { username: 'john' },
      { "$setOnInsert": { points: '0' },
            $push: {
            "boards": {
                "account":req.body.account,
                "game":req.body.game                                        
            }
        }          
      { upsert: true},
      function(err, doc) {    

      });
Run Code Online (Sandbox Code Playgroud)

  • 最好设置选项`{upsert:true,setDefaultsOnInsert:true}`,而不是为每个`findOneAndUpdate`调用重复相同的默认值.[见JohnnyHK对此问题的回答.](http://stackoverflow.com/a/25767756/404699) (3认同)