Answer the question
In order to leave comments, you need to log in
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 }
$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()
);
Answer the question
In order to leave comments, you need to log in
1. Why is timestamp stored and not Date?
2. Convert timestamp to Date:
new Date(timestamp)
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 questionAsk a Question
731 491 924 answers to any question