2015-10-09 73 views
1

我有一个默认生成值的模型,在整个文档生命周期中不会更改,除非在特殊情况下。Mongoose.js:属性默认值isModified标志

的文件可能会被标记为使用doc.update({_id: doc._id, deleted_at: new Date()}, {overwrite: true})

删除在一个非常特殊的情况下,可以将文档“起死回生” - 抬头通过它的id和之后再次与正在工作。

在预保存钩子中,每当创建或恢复文档时,我都需要执行一些操作(例如将文档存储在另一个集合中)。

考虑以下简化代码:

'use strict'; 
var mongoose = require('mongoose'); 
mongoose.connect('mongodb://localhost/test'); 
var someSchema = mongoose.Schema({ 
     immutable: { 
      type: String, 
      default: function() { 
       return 'SomeVeryRandomValue'; 
      } 
     } 
    }); 

someSchema.pre('save', function (next) { 
    if (this.isNew || this.isModified('immutable')) { 
     console.log('Processing pre-save hook!'); 
    } 
    next(); 
}); 

var SomeModel = mongoose.model('SomeModel', someSchema, 'test'); 

mongoose.connection.once('open', function (err) { 
    var testDoc = new SomeModel({}); 
    console.log('New: %j', testDoc.toObject()); 

    testDoc.save(function(err) { 
     console.log('Initial saved: %j', testDoc.toObject()); 

     testDoc.update({_id: testDoc._id}, {overwrite: true}, function (err) { 

      // at this point using mongo console: 
      // > db.test.findOne() 
      // { "_id" : ObjectId("5617b028bf84f0a93687cf67") } 

      SomeModel.findById(testDoc.id, function(err, reloadedDoc) { 
       console.log('Reloaded: %j', reloadedDoc.toObject()); 
       console.log('reloaded isModified(\'immutable\'): %j', reloadedDoc.isModified('immutable')); 

       reloadedDoc.save(function(err) { 
        console.log('Re-saved: %j', reloadedDoc); 
        mongoose.connection.close(); 
       }); 
      }); 

     }); 
    }); 
}); 

和脚本运行时输出:

$ node mongoose-modified-test.js 
New: {"_id":"5617b64c5376737b46f6bb98","immutable":"SomeVeryRandomValue"} 
Processing pre-save hook! 
Initial saved: {"__v":0,"_id":"5617b64c5376737b46f6bb98","immutable":"SomeVeryRandomValue"} 
Reloaded: {"_id":"5617b64c5376737b46f6bb98","immutable":"SomeVeryRandomValue"} 
reloaded isModified('immutable'): false 
Re-saved: {"_id":"5617b64c5376737b46f6bb98","immutable":"SomeVeryRandomValue"} 

immutable改性没有标记恕我直言应该 - 原始文件有该属性没有价值。

一个变通的解决办法是删除默认值immutable属性,并确定预验证挂机像这样的:

someSchema.pre('validate', function (next) { 
    if (this.isNew || !this.immutable) { 
     this.immutable = 'SomeVeryRandomValue'; 
    } 
    next(); 
}); 

这不是我所需要的东西,因为该值将不会产生直到我尝试验证/保存文档。前/后挂钩不会在new SomeModel({})上执行,所以我不能使用这些挂钩。

我应该为mongoose.js开一个问题吗?

+0

https://github.com/Automattic/mongoose/issues/3480 – geronime

回答

1

this.$isDefault('immutable')可以用来代替。脚本的

someSchema.pre('save', function (next) { 
    if (this.isNew || this.$isDefault('immutable')) { 
     console.log('Processing pre-save hook!'); 
    } 
    next(); 
}); 

输出与更新预存钩:

$ node --harmony mongoose-modified-test.js 
New: {"_id":"56276f0c1a2f17ae7e0a03f7","immutable":"SomeVeryRandomValue"} 
Processing pre-save hook! 
Initial saved: {"__v":0,"_id":"56276f0c1a2f17ae7e0a03f7","immutable":"SomeVeryRandomValue"} 
Reloaded: {"_id":"56276f0c1a2f17ae7e0a03f7","immutable":"SomeVeryRandomValue"} 
Processing pre-save hook! 
Re-saved: {"_id":"56276f0c1a2f17ae7e0a03f7","immutable":"SomeVeryRandomValue"} 

感谢@ vkarpov15为clarification