Question

I have the following mongo db schema

    {
        "user" : ObjectId("52aaea01701054a10a002212"),
        "type" :"go_to_page", 
        "params" : 
        { 
            "page" : "users"
        },
        "created" :ISODate("2013-12-30T05:30:24.324Z")
    }

The "page" can have any of the four values like profile, event, people and shout. So if a user jumps from people page to profile page i can save them into the mongo database and now i can have the log of the activities of the user. Now i have to find the exact time in minutes that the user spent on each of the above mentioned four pages.

is there any method in mongo which takes input two given dates and and returns the timespan between the two dates in minutes. i.e. if i give the data as 2014-04-29 23:50:00 and 2014-04-30 00:00:00 then it should return 10 because it is 10 minutes between the two given dates. Any help in this regard would be appreciated.

Was it helpful?

Solution

I tried following code in mongo 2.4.8 and found when you take difference between two dates it returns a result in millisecond,then wrote following function to get data in minutes. Here I have assumed you have two date fields in your document 'entered'(when user entered) and 'left'(when user left) and finally you calculate the difference in minutes and store 'timespent' in the same document

 db.collectioName.find().forEach(function(d){
    //you can use Math.ceil or Math.floor
    d.timespent=Math.round((d.visited-d.left)/60000); 
    db.collectioName.save(d);
 });

Hope it helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top