Saturngod

What's on my mind

Group By Date in mongodb

I am using mongodb for tracking my website. I am saving date and time with ISODate , "time" : ISODate("2014-05-18T13:23:04.227Z") .

So, I wrote like that.

db.analystic.aggregate(
{
    "$project": {
        "y": {
            "$year": "$time"
        },
        "m": {
            "$month": "$time"
        },
        "d": {
            "$dayOfMonth": "$time"
        }
    }
},
{
    "$group": {
        "_id": {
            "year": "$y",
            "month": "$m",
            "day": "$d"
        },
        count: {
            "$sum": 1
        }
    }
},
{
    $sort: {
        "_id.year": 1,
        "_id.month": 1,
        "_id.day": 1
    }
})

First , need to separate the year , month and date. After that , make a group and then sort it.

Install Mongodb PHP driver on Mac OS X

Before install the php driver , you must have brew or portin your mac os x. I am using brew in this example. If you are using port, just change brew to port.

 

Download the source

[gist id=”3607644” file=”gitmongod.txt”]

After finishing the installation , you need to edit php.ini
[gist id=”3607644” file=”editphp.txt”]
search extension=php_zip.dll in php.ini add extension = mongo.so in under the line of extension= php_zip.dll
So, it will be like that
[gist id=”3607644” file=”php.ini”]
Save the file with :wq Restart the apache server [gist id=”3607644” file=”restartapache”]
Done. You can check mongodb in phpinfo(); and you should see like following
 
Screen_shot_2012-08-31_at_12