npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

aggregate-reduce

v0.0.2

Published

Perform aggregate reduce operations on MongoDB.

Downloads

1

Readme

aggregate-reduce

Perform aggregate reduce operations on MongoDB.

Mongo's aggregation framework is way faster than MapReduce, however it lacks the ability to reduce aggregations incrementally.

This module adds the ability to perform aggregations and reduce it into a collection of a choice.

Usage:

aggregateReduce(collection, pipelineArray, map, reduce, options)

Calling this function will perform the following pipeline on the given collection:

  1. Aggregate to a temporary collection
  2. Perform MapReduce on the temporary collection.
  3. Dispose the temporary collection

Sidenote: this module uses tempcol, which handles the removal of collections even on cases of errors.

Temporary collections would be prefixed as tmp.ar.

args

  • collection - The collection to perform Aggregate/Reduce on.
  • pipelineArray - as you would do with the aggregation framework.
  • map - map function, as you would do with mapReduce.
  • reduce - reduce function, as you would do with mapReduce.
  • options - options object.

options

  • All options that are supported in the aggregation framework, except of out.
  • All options that are supported in mapReduce
  • ttl - abort after X amount of seconds. (default=3600).

####Example

You can run the following example several times, it will reduce the results to tstReduce collection incrementally.

var MongoClient = require('mongodb').MongoClient;
var aggregateReduce = require('aggregate-reduce');

MongoClient.connect('YOUR MONGO URI',{}, function (err, db) {
  if (err) return console.error(err);

  // Let's create a test data set and insert 1K rows
  var col = db.collection('test');
  var timeStamp = new Date().getTime();

  var i = 0;

  ;(function insert(){
    var d = new Date(2014, Math.round(Math.random() * 11 +1), Math.round(Math.random() * 30 +1))
    var obj = {
      date: d,
      rand: Math.round(Math.random()*100),
      timestamp: timeStamp
    }

    col.insert(obj, function (err) {
      if (err) {console.error(err); return db.close();}
      console.log('inserted', obj)

      if (++i < 1000) return insert();

      console.log('done');
      var map = function () {emit(this._id, this)};
      var reduce = function (key, values) {
        var obj = values[0];
        for (var i=1; i<values.length; ++i) {
          obj.count = (obj.count || 0) + values[i].count;
          obj.sum = (obj.sum || 0) + values[i].sum;
        }
        return obj;
      }

      aggregateReduce(col,[
        {$match: {timestamp:{$gte: timeStamp}}},
        {$group:{
          _id:{month:{$month: '$date'}, day:{$dayOfMonth:'$date'}},
          count: {$sum: 1},
          sum: {$sum: '$rand'}
        }}
      ],map, reduce, {out:{reduce: 'tstReduce'}}, function (err, col) {
        if(err) return console.error(err);
        col.find().stream().on('data', console.log);
      })
    })
  })();
});

install

With npm do:

npm install aggregate-reduce

license

MIT