通常採用的是set方法,這個方法只針對model.save()和create()適用,而對於update,findOneAndUpdate等不適用javascript
DeviceSchema.path('name').get(utils.aesDecrypt);
DeviceSchema.path('deviceName').get(utils.aesDecrypt);
DeviceSchema.path('position').get(utils.aesDecrypt);
DeviceSchema.path('name').set(utils.aesEncrypt);
DeviceSchema.path('deviceName').set(utils.aesEncrypt);
DeviceSchema.path('position').set(utils.aesEncrypt);
官網中提供了:
schema.pre('update', function() { this.update({},{ $set: { updatedAt: new Date() } }); });
可是這個方法不頂用.
若是有較好的方法,請你們一塊兒提供.
mongoose的get方法,對原生mongodb取出來的的值進行更改,採用path或者在schema上定義.
獲取時,注意要使用 toObject({getters: true})var schema = new Schema({ name: String }); schema.path('name').get(function (v) { return v + ' is my name'; });