Aggregate on string length in MongoDB

It's not actually possible to use the aggregation pipeline to aggregate on string length, at last in MongoDB 3.2 (the current version as of this writing). You may think that you can use the $size operator, but alas you'll end up with errors that look like

The argument to $size must be an Array, but was of type: EOO

The problem I was trying to solve was to determine do a aggregation on string lengths to determine how many documents had a field of a certain length. Fortunately, this is perfectly suited for Map-Reduce.

Here's all you need:

  function() {
    emit(this.upc_no ? this.upc_no.length : 0, 1);
  function(key,values) {
    return Array.sum(values);
  { "out": { "inline": 1 } }

The above code will emit a key-value pair with the length of the upc_no field for each document. This is the map function. The second function simply sums all of the values that match that key (this is the reduce function). Finally, it will output the results inline.

comments powered by Disqus