Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I see you are asking about a PHP issue, but you are using javascript to ask, so I’m assuming a javascript answer will help you move things along. As such here is the javascript needed in the shell to run your aggregation. I strong suggest getting your aggregation working in the shell(or some other javascript editor) in general and then translating it into the language of your choice. It is a lot easier to see what is going on and there faster using this method. You can then run:</p> <pre><code>use admin db.runCommand( { setParameter: 1, logLevel: 2 } ) </code></pre> <p>to check the bson output of your selected language vs what the shell looks like. This will appear in the terminal if mongo is in the foreground, otherwise you’ll have ot look in the logs.</p> <p>Summing the routes in the aggregation framework [AF] with Mongo is fairly strait forward. The AF is faster and easier to use then map reduce[MR]. Though in this case they both have similar issues, simply pushing to an array won’t yield a count in and of itself (in MR you either need more logic in your reduce function or to use a finalize function).</p> <p>With the AF using the example data provided this pipeline is useful:</p> <pre><code>db.agg1.aggregate([ {$group:{ _id: { city: "$city", to: "$to" }, count: { $sum: 1 } }}, {$group: { _id: "$_id.city", to:{ $push: {to: "$_id.to", count: "$count"}} }} ]); </code></pre> <p>The aggregation framework can only operate on known fields, but many pipeline operations so a problem needs to broken down with that as a consideration. Above, the 1st stage calculates the numbers need, for which there are 3 fixed fields: the source, the destination, and the count. The second stage has 2 fixed fields, one of which is an array, which is only being pushed to (all the data for the final form is there).</p> <p>For MR you can do this:</p> <pre><code>var map = function() { var key = {source:this.city, dest:this.to}; emit(key, 1); }; var reduce = function(key, values) { return Array.sum(values); }; </code></pre> <p>A separate function will have to pretty it however.</p> <p>If you have any additional questions please don’t hesitate to ask.</p> <p>Best, Charlie</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload