Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since <code>entities.user_mentions</code> is an array, you want to emit a value for each screen_name in the <code>map()</code>:</p> <pre><code>var map = function() { this.entities.user_mentions.forEach(function(mention) { emit(mention.screen_name, { count: 1 }); }) }; </code></pre> <p>Then count the values by unique screen_name in the <code>reduce()</code>:</p> <pre><code>var reduce = function(key, values) { // NB: reduce() uses same format as results emitted by map() var result = { count: 0 }; values.forEach(function(value) { result.count += value.count; }); return result; }; </code></pre> <p>Note: to debug your map/reduce JavaScript functions, you can use <code>print()</code> and <code>printjson()</code> commands. The output will appear in your <code>mongod</code> log.</p> <p>EDIT: For comparison, here is an example using the new <a href="http://docs.mongodb.org/manual/reference/aggregation/" rel="nofollow noreferrer">Aggregation Framework</a> in MongoDB 2.2:</p> <pre><code>db.twitter_sample.aggregate( // Project to limit the document fields included { $project: { _id: 0, "entities.user_mentions" : 1 }}, // Split user_mentions array into a stream of documents { $unwind: "$entities.user_mentions" }, // Group and count the unique mentions by screen_name { $group : { _id: "$entities.user_mentions.screen_name", count: { $sum : 1 } }}, // Optional: sort by count, descending { $sort : { "count" : -1 }} ) </code></pre> <p>The original Map/Reduce approach is best suited for a large data set, as is implied with Twitter data. For a comparison of Map/Reduce vs Aggregation Framework limitations see the related discussion on the StackOverflow question <a href="https://stackoverflow.com/questions/12337319/mongodb-group-group-and-mapreduce/12340283#12340283">MongoDB group(), $group and MapReduce</a>.</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. 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