Skip to content

Very simple implementation of some of mongodb aggregation framework functions for Meteor

Notifications You must be signed in to change notification settings

monbro/meteor-mongodb-mapreduce-aggregation

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

meteor-mongodb-mapreduce-aggregation

Very simple implementation of some of mongodb aggregation framework functions for Meteor.

meteor-mongodb-mapreduce-aggregation is a fork of meteor-mongo-server that do not expose the aggregation framework to the client, being available only on server side.

Unfortunately the original source was not working for me anymore as a plugin and seems to be not maintained. So I created this plugin and bugfixed it.

It extends Collection with 3 methods so far, mapReduce, distinct and aggregate, so that you can do:

    col = new Meteor.Collection "name"

    if Meteor.isServer
        # mapReduce
        map = function() {emit(this.Region, this.Amount);}
        reduce = function(reg, am) { return Array.sum(am);};

        col.mapReduce map, reduce, {out: "out_collection_name", verbose: true}, (err,res)->
            console.dir res.stats # statistics object for running mapReduce

        # distinct
        result = col.distinct "Field Name"
        console.dir result

        #aggregate
        result = col.aggregate pipeline
        console.dir result

Another mapReduce example in javascript:

    // on the server side
    /////////////////////

    Posts = new Mongo.Collection("Posts");
    Tags = new Mongo.Collection("Tags");

    Meteor.methods({

        mostUsedTags: function() {
            var map = function() {
                if (!this.tags) {
                    return;
                }

                for (index in this.tags) {
                    emit(this.tags[index], 1);
                }
            }

            var reduce = function(previous, current) {
                var count = 0;

                for (index in current) {
                    count += current[index];
                }

                return count;
            }

            // keep in mind that executing the mapReduce function will override every time the collection Tags
            var result = Posts.mapReduce(map, reduce, {query: {}, out: "Tags", verbose: true});

            // now return all the tags, ordered by usage
            // as an alternative solution you can also publish the collection Tags and use this one at the client side
            return Tags.find({},{ sort: {'value': -1}, limit:10}).fetch();
        }

    });

    // on the client side you could do
    //////////////////////////////////

    Meteor.call("mostUsedTags", function(err, data) {
        console.log(data);
    });

To install it, run:

$ meteor add monbro:mongodb-mapreduce-aggregation

This package is MIT Licensed. Do whatever you like with it but any responsibility for doing so is your own.

About

Very simple implementation of some of mongodb aggregation framework functions for Meteor

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • CoffeeScript 74.4%
  • JavaScript 25.6%