Thursday 18 May 2017

How to have an auto calculated attribute in mongoose schema

suppose I have following mongoose schema in Node.js

var schema = new mongoose.Schema({
    views:Number,
    clicks:Number,
    ctr:Number
})

In which I want that CTR attribute should be automatically calculated by a formula based on values of other attributes, i.e lets say

ctr = clicks / views

I don't want to have CTR as a virtual field because based on the value of CTR I will sort all documents of my collection and apparently sorting is not possible based on virtual fields. So I was wondering if there is any way to have calculated attributes in mongoose schema which gets automatically updated on update of any of the dependent attribute/field



via Point Networks

No comments:

Post a Comment