Giter Site home page Giter Site logo

meteor-reactive-aggregate's Introduction

meteor-reactive-aggregate

Reactively publish aggregations.

meteor add jcbernack:reactive-aggregate

This helper can be used to reactively publish the results of an aggregation.

Usage

ReactiveAggregate(sub, collection, pipeline, options)
  • sub should always be this in a publication.
  • collection is the Mongo.Collection instance to query.
  • pipeline is the aggregation pipeline to execute.
  • options provides further options:
    • observeSelector can be given to improve efficiency. This selector is used for observing the collection. If none is given any change to the collection will cause the aggregation to be reevaluated.
    • clientCollection defaults to collection._name but can be overriden to sent the results to a different client-side collection.

Quick Example

A publication for one of the examples in the MongoDB docs would look like this:

Meteor.publish("booksByAuthor", function () {
  ReactiveAggregate(this, Books, [{
    $group: {
      _id: "$author",
      books: { $push: "$$ROOT" }
    }
  }]);
});

Extended Example

Define the parent collection you want to run an aggregation on. Let's say:

Reports = new Meteor.Collection('Reports');

.. in a location where all your other collections are defined, say lib/collections.js

Next, prepare to publish the aggregation on the Reports collection into another client-side-only collection we'll call, clientReport.

Create the clientReport in the client side (its needed only for client use). This collection will be the destination in which the aggregation will be put into upon completion.

Now you publish the aggregation on the server:

Meteor.publish("reportTotals", function() {
// Remember, ReactiveAggregate doesn't return anything
ReactiveAggregate(this, Reports, [{
    // assuming our Reports collection have the fields: hours, books
    $group: {
        '_id': this.userId,
        'hours': {
        // In this case, we're running summation. 
            $sum: '$hours'
        },
        'books': {
            $sum: 'books'
        }
    }
}, {
    $project: {
    	// an id can be added here, but when omitted, 
        // it is created automatically on the fly for you
        hours: '$hours',
        books: '$books'
    } // Send the aggregation to the 'clientReport' collection available for client use
}], { clientCollection: "clientReport" });
});

We therefore need to subscribe to the above Publish.

Meteor.subscribe("reportTotals");

Then in our Template helper:

Template.statsBrief.helpers({
    reportTotals: function() {
        console.log("I'm working");
        return clientReport.find();
    },
});

Finally, your template:

{{#each reportTotals}}Total Hours: {{hours}} <br/>Total Books: {{books}}{{/each}}

Your aggregated values will therefore be available in client-side and behave reactively just as you'd expect.

Enjoy aggregating reactively!

meteor-reactive-aggregate's People

Contributors

jcbernack avatar seanmavley avatar

Watchers

James Cloos avatar Andy Edwards avatar  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.