0

in php i have created a cursor which hold group by result from MongoDB with following command.

$m = new MongoClient();
$db = $m->Forensic;
$c= $db->mobile_data;

// JavaScript function to perform
$reduce = "function (obj, prev) { prev.count++; }";

//group by source (number of messages);
$d = $c->group( ["Source" => 1], ["count" => 0], $reduce);

how can I sort my result based on "Source" key.

Thnaks

2 Answers 2

0

I would refer you to this question and see if that helps. There is also a link to the MongoDB documentation that might help you as well. There is also a decent explanation here also.

I'm just dabbling in group and sort in MongoDB myself.

One example I found was;

db.orders.aggregate([
   {
     $group: {
        _id: "$cust_id",
        count: { $sum: 1 }
     }
   },
   { $match: { count: { $gt: 1 } } }
])

and there is also a similar thread here as well.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer but I know how to sort result from mongodb platform, but currently I am passing my command from php. how to write code in php group by sort would be much appreciate
In PHP or on SQL?
PHP commad like $d = $c->group( ["Source" => 1], ["count" => 0], $reduce, Sort("Source"));
0

Found an alternative solution

$m = new MongoClient();
$db = $m->Forensic;
$c= $db->mobile_data;

// JavaScript function to perform
$reduce = "function (obj, prev) { prev.count++; }";

//group by source (number of messages);
$d = $c->group( ["Source" => 1], ["count" => 0], $reduce);

//sort record
sort($d['retval']);

which will sort by Source field

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.