Responsive Ad Area

Share This Post

test

Mongoose own schema methods don’t work with aggregation framework results

I am using the MEAN stack, and I have defined some own functions for my models in mongoose:

DemoSchema.method({
    tsFromID: function() {
        var datenow = +new Date(+new Date()+60*1000*60*2);
        var dateless15 = datenow - 1000*60*15;
        //console.log(new Date(datenow));
        //console.log(new Date(dateless15));
        //ist das als 15 Minuten?
        return +this.Timestamp >= +dateless15;},
    checkID: function() {return this._id;}
});

now, If I execute a query with mongoose / MongoDB to get document results back,
I can only apply those methods with resultsets that were executed (found) by the “find” query method:

DemoSchema.find({}).exec(function(err,result) {
        for (elem of result) {
            console.log("Alter:", elem.tsFromID())
        }
    }),..

However, if I try to apply the method on document results that came from the aggregation framework, the methods don’t work on the result documents, it says that “method”… is not a function. Why ? The returned documents from aggregation are of cursor type objects? Or what’s the difference? below is an example..

DemoSchema.aggregate([      
        {
            $addFields: {
                subID:  {
                    "$eq": [{"$trunc": {"$divide": ["$_id", 1000000]}},+req.params.demo_id]
                }
            }
        },      
        {
            "$match": { subID:true }
        },
        {
            "$sort": {"Timestamp":-1}
        },
        {
            "$limit": 1
        }
        /*
        {
            "$group": {
                "_id": "$subID",
                lastID : {$first: "$Timestamp"}
            }
        }
*/
    ]).exec(function(err, flPot) {
        console.log("Anzahl gefundener Dokumente: ", flPot.length)
        res.json(flPot)
        console.log("Timestamp < 15 min ?", flPot.tsFromID())
        for (let elem of flPot) {
            console.log(elem);
            console.log("Timestamp: ", elem.Timestamp)
            console.log("Jünger als 15 min?:", elem.tsFromID()) 
        }
    }
    )


Mongoose own schema methods don’t work with aggregation framework results
Mongoose own schema methods don’t work with aggregation framework results
test
{$excerpt:n}

Share This Post

Leave a Reply

Your email address will not be Publishedd. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Skip to toolbar