Skip to content

takeru/meteor-mongo-server

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

46 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

meteor-mongodb-mapreduce-aggregation

An authoritative MongoDB aggregate, mapReduce and distinct package for Meteor. This differs from other packages by including tests and letting you pass options to calls to aggregate.

Documentation

Mongo.Collection on the server is extended with with 3 methods: mapReduce, distinct and aggregate. You can specify options for aggregate when using MongoDB 2.6 or later in hosted environments.

When specifying options, make sure to include a readPreference field, e.g., {readPreference: 'primary'}. Read more about read preferences.

    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 doctorpangloss:mongodb-mapreduce-aggregation

This package is MIT Licensed.

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • CoffeeScript 54.5%
  • JavaScript 45.5%