Giter Site home page Giter Site logo

takeru / meteor-mongo-server Goto Github PK

View Code? Open in Web Editor NEW

This project forked from hiddenswitch/meteor-mongo-server

0.0 2.0 0.0 120 KB

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

License: MIT License

JavaScript 45.53% CoffeeScript 54.47%

meteor-mongo-server's Introduction

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.

meteor-mongo-server's People

Contributors

jhoxray avatar doctorpangloss avatar zvictor avatar demyanrogozhin avatar vraptor75011 avatar lalomartins avatar

Watchers

sasaki takeru avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.