【发布时间】:2014-04-16 04:00:49
【问题描述】:
我正在寻找一种方法让 Mongoose 使用生成的值自动填充字段。我认为以下方法会起作用,但这是不行的。是否可以在保存时自动填充子架构字段? (不寻找中间件代码 - 只是自动的)
// A function to gen an integer
function randomValue () {
var s = parseInt(crypto.randomBytes(4).toString('hex'), 16);
return s;
}
// Subschema
var Variation = new Schema({
// Set default to gen integer
code: {
type: Number,
default: randomValue
}
},{ _id : false });
// Main schema
var Product = new Schema({
variations: {
type: [Variation]
}
});
【问题讨论】:
标签: javascript node.js mongodb mongoose