Populate making use of Mongo-stored calculations and formulas?

41 views
Skip to first unread message

Hermann Schliederwirte

unread,
Nov 6, 2014, 9:26:17 AM11/6/14
to mongoos...@googlegroups.com
I am designing a rather complex financial application where we have to perform  multiple calculations on specific Mongo documents, in a very simplified example:

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

Related Post: Also see http://stackoverflow.com/questions/26778369/where-in-mongoose-or-mongodb-to-put-calculations-and-formulas

Hermann Schliederwirte

unread,
Nov 6, 2014, 11:37:09 AM11/6/14
to mongoos...@googlegroups.com
Ok I am a few steps further by defining "virtuals", see here: http://mongoosejs.com/docs/guide.html

This works marvously, but now I am thinking about pushing the domain logic into the database as well, in order to have a complete universal system free of domain logic, only dependent on user(admin) formulae input in the front end.

e.g.:

// 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



Obviously above prints "this.test.value (0.02*this.test.value") is the computed result". How to achieve the desired functionality here?

Hermann Schliederwirte

unread,
Nov 6, 2014, 12:45:47 PM11/6/14
to mongoos...@googlegroups.com

Andreas R

unread,
Nov 6, 2014, 3:55:24 PM11/6/14
to mongoos...@googlegroups.com
Hello Hermann,

This Mongoose is an embeddable web server in C. Your Mongoose is in
another castle - I mean, on another mailing list ;).

Greetings,

Andreas

2014-11-06 18:45 GMT+01:00 Hermann Schliederwirte <nottin...@gmail.com>:
> Also see:
> http://docs.mongodb.org/manual/tutorial/store-javascript-function-on-server/
>
> --
> You received this message because you are subscribed to the Google Groups
> "mongoose-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mongoose-user...@googlegroups.com.
> To post to this group, send email to mongoos...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mongoose-users.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages