Note that there are some explanatory texts on larger screens.

plurals
  1. POMongoDB Map Reduce newbie (PHP)
    primarykey
    data
    text
    <p>I'm new to the map reduce concept and even though I'm making some slow progress, I'm finding some issues that I need some help with.</p> <p>I have a simple collection consisting of an id, city and and destination, something like this:</p> <pre><code>{ "_id" : "5230e7e00000000000000000", "city" : "Boston", "to" : "Chicago" }, { "_id" : "523fe7e00000000000000000", "city" : "New York", "to" : "Miami" }, { "_id" : "5240e1e00000000000000000", "city" : "Boston", "to" : "Miami" }, { "_id" : "536fe4e00000000000000000", "city" : "Washington D.C.", "to" : "Boston" }, { "_id" : "53ffe7e00000000000000000", "city" : "New York", "to" : "Boston" }, { "_id" : "5740e1e00000000000000000", "city" : "Boston", "to" : "Miami" }, ... </code></pre> <p>(Please do note that this data is just made up for example purposes)</p> <p>I'd like to group by city the destinations including a count:</p> <pre><code>{ "city" : "Boston", values : [{"Chicago",1}, {"Miami",2}] } { "city" : "New York", values : [{"Miami",1}, {"Boston",1}] } { "city" : "Washington D.C.", values : [{"Boston", 1}] } </code></pre> <p>For this I'm starting to playing with this function to map:</p> <pre><code> function() { emit(this.city, this.to); } </code></pre> <p>which performs the expected grouping. My reduce function is this:</p> <pre><code> function(key, values) { var reduced = {"to":[]}; for (var i in values) { var item = values[i]; reduced.to.push(item); } return reduced; } </code></pre> <p>which gives somewhat an expected output:</p> <pre><code>{ "_id" : ObjectId("522f8a9181f01e671a853adb"), "value" : { "to" : [ "Boston", "Miami" ] } } { "_id" : ObjectId("522f933a81f01e671a853ade"), "value" : { "to" : [ "Chicago", "Miami", "Miami" ] } } { "_id" : ObjectId("5231f0ed81f01e671a853ae0"), "value" : "Boston" } </code></pre> <p>As you can see, I still haven't counted the repeated cities, but as can be seen above, for some reason the last result in the output doesn't look good. I'd expected it to be </p> <pre><code>{ "_id" : ObjectId("5231f0ed81f01e671a853ae0"), "value" : { "to" : ["Boston"] } } </code></pre> <p>Has this anything to do with the fact that there is a single item? Is there any way to obtain this?</p> <p>Thank you.</p>
    singulars
    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. 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