I
I
iamsaint2013-02-22 17:03:21
PHP
iamsaint, 2013-02-22 17:03:21

Grouping in MongoDB

Good afternoon.
I have a collection like this:

{ "_id" : ObjectId("50d2aed37f1515b621000001"), "user_id" : "274",  "timestamp" : 1355984595 }
{ "_id" : ObjectId("50d2cd047f1515b414000001"), "user_id" : "450",  "timestamp" : 1355992324 }
{ "_id" : ObjectId("50d2ed4c7f1515d308000001"), "user_id" : "450",  "timestamp" : 1356000588 }

In php, I make a selection grouped by user_id in the following way:
        $connection = new Mongo();
        $db = $connection->test;
        $collection = $db->click;

        $res = $collection->group(
            array(
                'user_id'=>true
            )
            ,array('count' => 0)
            ,'function (obj, prev) {
                ++prev.count;
            }'
            ,array()
        );

How can I add a grouping condition on the timestamp field so that the resulting timestamp field is in the format dd.mm.yyyy?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vyacheslav Slinko, 2013-02-22
@KeepYourMind

1. Why is timestamp stored and not Date?
2. Convert timestamp to Date:

new Date(timestamp)

P
PaulOkopny, 2013-02-22
@PaulOkopny

In general, the basic recommendation for working with mongo (if I remember correctly, it comes from the developers themselves): store data in the form in which you plan to present it. Therefore:
1. Add a date field in the required format.
2. Group by it manually or by Aggregation Framework .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question