SchemaType.prototype.transform()
Parameters
- fn «Function»
Returns:
- «SchemaType» this
Defines a custom function for transforming this path when converting a document to JSON.
Mongoose calls this function with one parameter: the current value
of the path. Mongoose then uses the return value in the JSON output.
Example:
const schema = new Schema({
date: { type: Date, transform: v => v.getFullYear() }
});
const Model = mongoose.model('Test', schema);
await Model.create({ date: new Date('2016-06-01') });
const doc = await Model.findOne();
doc.date instanceof Date; // true
doc.toJSON().date; // 2016 as a number
JSON.stringify(doc); // '{"_id":...,"date":2016}'
当前内容版权归 mongoosejs 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 mongoosejs .