var NumSchema = mongoose.Schema({
{
a: { type: Number, default: 1 },
b: { type: Number, default: 1 }
sum: { type: mongoose.Schema.Types.Number, ref: 'CALC' }
}
})
module.exports = mongoose.model('NUMB', NumSchema);
var FuncSchema
= mongoose.Schema({
{
sum: { type: Number },
diff: { type: Number }
}
})
module.exports = mongoose.model('FUNC', FuncSchema);
CALC is then stored in Mongo as e.g.:
{
"sum": "NUMB.a + NUMB.b",
"diff": "NUMB.a - NUMB.b"
}
Why all this? Think Excel. Users will be able to create functions and apply them to certain subdocuments ("columns").
Questions:
- When using populate as above, how do I tell Mongoose whether to use func1 or to use func2?
- How could a correctly working implementation of above scenario look like? Any code, pointers or ideas are appreciated.
Also see: http://mongoosejs.com/docs/guide.html
// create a document
var doc = new DocSchema({
test: { value: "500" },
functions: {
func1: "this.test.value (0.02*this.test.value")
}
});
console.log('%s is the computed result', doc.functions.func1); // this.test.value (0.02*this.test.value") is the computed result