### Install tunguska-reactive-aggregate Meteor Package Source: https://github.com/robfallows/tunguska-reactive-aggregate/blob/master/README.md This command adds the `tunguska:reactive-aggregate` package to your Meteor project, enabling reactive publication of MongoDB aggregation results. It integrates the package into your application's dependencies. ```Shell meteor add tunguska:reactive-aggregate ``` -------------------------------- ### Install simpl-schema for Mongo.ObjectID Handling Source: https://github.com/robfallows/tunguska-reactive-aggregate/blob/master/README.md To ensure full support for Mongo.ObjectID types within your collections when using `tunguska-reactive-aggregate`, install the `simpl-schema` npm package. This is crucial for proper handling of embedded ObjectID fields. ```Shell meteor npm i simpl-schema ``` -------------------------------- ### Install lodash or lodash-es for Mongo.ObjectID Handling Source: https://github.com/robfallows/tunguska-reactive-aggregate/blob/master/README.md For comprehensive support of Mongo.ObjectID types, particularly for embedded fields, install either `lodash-es` or `lodash` via npm. These packages provide necessary utility functions for `tunguska-reactive-aggregate`'s ObjectID workaround. ```Shell meteor npm i lodash-es ``` ```Shell meteor npm i lodash ``` -------------------------------- ### Publish Aggregated Books by Author in Meteor Source: https://github.com/robfallows/tunguska-reactive-aggregate/blob/master/README.md A quick example demonstrating how to use ReactiveAggregate within a Meteor publication to group documents by author and push the root documents into an array, mimicking a MongoDB aggregation example. ```js Meteor.publish("booksByAuthor", function () { ReactiveAggregate(this, Books, [{ $group: { _id: "$author", books: { $push: "$$ROOT" } } }]); }); ``` -------------------------------- ### Define MongoDB Collection for Reports in Meteor Source: https://github.com/robfallows/tunguska-reactive-aggregate/blob/master/README.md Defines a new MongoDB collection named 'Reports' using Meteor's Mongo package. This collection serves as the parent collection for running aggregations in the extended example. ```js import { Mongo } from 'meteor/mongo'; export const Reports = new Mongo.Collection('Reports'); ``` -------------------------------- ### ReactiveAggregate Function Parameters Source: https://github.com/robfallows/tunguska-reactive-aggregate/blob/master/README.md Detailed documentation for the parameters of the `ReactiveAggregate` function, including `context`, `collection`, `pipeline`, and `options`. ```APIDOC ReactiveAggregate(context, collection, pipeline, options) context: 'this' Description: Should always be 'this' in a publication. collection: Mongo.Collection instance Description: The Mongo.Collection instance to query. An observer is automatically added on this collection, unless options.noAutomaticObserver is set to 'true'. Deprecated Options: observeSelector, observeOptions (will be honored on automatically added observer, but recommended to use options.observers instead). pipeline: Array | Function Description: The aggregation pipeline to execute or a function that returns the aggregation pipeline. If a function, it will be called before updates triggered by observers. options: Object Description: Provides further options for the aggregation. ``` -------------------------------- ### ReactiveAggregate Options Object Properties Source: https://github.com/robfallows/tunguska-reactive-aggregate/blob/master/README.md Detailed documentation for the various properties available within the `options` object passed to `ReactiveAggregate`, controlling behavior like debouncing, debugging, and observer management. ```APIDOC options: aggregationOptions: Object Description: Can be used to add further, aggregation-specific options. See standard aggregation options for more information. capturePipeline: Function (docs: Array) Description: A callback function having one parameter which will return the array of documents comprising the current pipeline execution. Use with caution: this callback will be executed each time the pipeline re-runs. clientCollection: String Description: Defaults to the same name as the original collection, but can be overridden to send the results to a differently named client-side collection. debounceCount: Integer Description: An integer representing the number of observer changes across all observers before the aggregation will be re-run. Defaults to 0. Used in conjunction with debounceDelay to fine-tune reactivity. debounceDelay: Integer Description: An integer representing the maximum number of milli-seconds to wait for observer changes before the aggregation is re-run. Defaults to 0. Used in conjunction with debounceCount to fine-tune reactivity. debug: Boolean | Function (explainResult: Object) Description: A boolean (true or false), or a callback function having one parameter which will return the aggregate#cursor.explain() result. Defaults to false. objectIDKeysToRepair: Array Description: An array of SimpleSchema-style dotted path keys to fields of the schema that are Mongo.ObjectIDs. This is not needed by default and should not be used unless the default behaviour of the code fails in some way. Defaults to []. noAutomaticObserver: Boolean Description: Set this to true to prevent the backwards-compatible behaviour of an observer on the given collection. observers: Array Description: An array of cursors. Each cursor is the result of a Collection.find(). Each of the supplied cursors will have an observer attached, so any change detected will re-run the aggregation pipeline. ``` -------------------------------- ### Basic Usage of ReactiveAggregate in Meteor Publication Source: https://github.com/robfallows/tunguska-reactive-aggregate/blob/master/README.md Demonstrates how to import and use `ReactiveAggregate` within a Meteor publication, showing the basic signature with `context`, `collection`, `pipeline`, and `options`. ```javascript import { ReactiveAggregate } from 'meteor/tunguska:reactive-aggregate'; Meteor.publish('nameOfPublication', function() { ReactiveAggregate(context, collection, pipeline, options); }); ``` -------------------------------- ### ReactiveAggregate Configuration Options and Deprecated Parameters Source: https://github.com/robfallows/tunguska-reactive-aggregate/blob/master/README.md Details the various configuration options for ReactiveAggregate, including settings for ObjectId support and warning suppression. It also highlights deprecated parameters like observeSelector and observeOptions, advising their replacement with the observers option. ```APIDOC Options: - loadObjectIdModules (boolean): If true, tries to load modules necessary for ObjectId support. Defaults to true. - warnings (boolean): If false, suppresses all warnings, regardless of any specificWarnings. Defaults to true (warning messages are logged). - specificWarnings (object): Allows suppressing specific types of warnings (all default to true, warning messages are logged): - deprecations (boolean): Warnings about deprecations. - objectId (boolean): Warnings related to ObjectID and dependencies for using it. Deprecated Parameters: - observeSelector (object): Used for observing the collection to improve efficiency. (e.g., { authorId: { $exists: 1 } }) - observeOptions (object): Used to limit fields for further efficiency. (e.g., { limit: 10, sort: { createdAt: -1 } }) Note: Deprecated parameters are absorbed into the 'observers' option and should be replaced by adding cursors to the 'observers' array. Setting them to anything other than an empty object will result in a deprecation notice. ``` -------------------------------- ### Client-side Subscription and Display of Aggregated Reports Source: https://github.com/robfallows/tunguska-reactive-aggregate/blob/master/README.md Illustrates the client-side implementation for consuming the 'reportTotals' publication. This includes defining a client-only collection, subscribing to the aggregation, creating a template helper to retrieve the data, and rendering it in a Handlebars template. ```js import { Mongo } from 'meteor/mongo'; // Define a named, client-only collection, matching the publication's clientCollection. const clientReport = new Mongo.Collection('clientReport'); Template.statsBrief.onCreated(function() { // subscribe to the aggregation this.subscribe('reportTotals'); }); // Then in our Template helper: Template.statsBrief.helpers({ reportTotals() { return clientReport.find(); } }); ``` ```handlebars {{#each report in reportTotals}}
Total Hours: {{report.hours}}
Total Books: {{report.books}}
{{/each}} ``` -------------------------------- ### Capture Aggregation Pipeline Results Source: https://github.com/robfallows/tunguska-reactive-aggregate/blob/master/README.md Illustrates how to use the `capturePipeline` callback to receive the array of documents resulting from the current pipeline execution. Note: This callback runs on every re-run. ```javascript ReactiveAggregate(this, collection, pipeline, { capturePipeline(docs) { console.log(docs); }, }); ``` -------------------------------- ### Reactive Aggregation with $lookup and Custom Observers Source: https://github.com/robfallows/tunguska-reactive-aggregate/blob/master/README.md Demonstrates how to use `ReactiveAggregate` with the `$lookup` operator to join collections. It shows how to specify custom observers for disparate collections (`Authors` and `Books`) to trigger re-runs of the aggregation, rather than relying solely on the base collection. Includes `debounceCount` and `debounceDelay` options for controlling reactivity, ensuring changes are only made available after a certain number of updates or a delay. ```JavaScript Meteor.publish("biographiesByWelshAuthors", function () { ReactiveAggregate(this, Authors, [{ $lookup: { from: "books", localField: "_id", foreignField: "author_id", as: "author_books" } }], { noAutomaticObserver: true, debounceCount: 100, debounceDelay: 100, observers: [ Authors.find({ nationality: 'welsh'}), Books.find({ category: 'biography' }) ] }); }); ``` -------------------------------- ### Publish Aggregated Report Totals to Client-Side Collection Source: https://github.com/robfallows/tunguska-reactive-aggregate/blob/master/README.md Demonstrates publishing aggregated data from the 'Reports' collection to a client-side-only collection named 'clientReport'. The aggregation groups by user ID and sums 'hours' and 'books' fields, then projects the results. ```js Meteor.publish("reportTotals", function() { 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 by using the clientCollection property of options. }], { clientCollection: 'clientReport' }); }); ``` -------------------------------- ### Configure Aggregation-Specific Options Source: https://github.com/robfallows/tunguska-reactive-aggregate/blob/master/README.md Shows how to pass additional MongoDB aggregation-specific options like `maxTimeMS` and `bypassDocumentValidation` using the `aggregationOptions` property within the `ReactiveAggregate` options. ```javascript ReactiveAggregate(this, collection, pipeline, { aggregationOptions: { maxTimeMS: 500, bypassDocumentValidation: true }, }); ``` -------------------------------- ### Specify Client-Side Collection Name Source: https://github.com/robfallows/tunguska-reactive-aggregate/blob/master/README.md Demonstrates how to override the default client-side collection name using `clientCollection`, allowing results to be sent to a differently named collection on the client. ```javascript ReactiveAggregate(this, collection, pipeline, { clientCollection: "clientCollectionName", }); ``` -------------------------------- ### Triggering On-Demand Aggregations Source: https://github.com/robfallows/tunguska-reactive-aggregate/blob/master/README.md Shows how to configure an aggregation to re-run only when an arbitrary, independent collection is mutated. By observing a specific document in the `Reruns` collection, the aggregation can be triggered manually, providing a mechanism for on-demand reactivity. This mutation can be performed via a Meteor Method or pub/sub. ```JavaScript Meteor.publish("biographiesByWelshAuthors", function () { ReactiveAggregate(this, Authors, [{ $lookup: { from: "books", localField: "_id", foreignField: "author_id", as: "author_books" } }], { noAutomaticObserver: true, observers: [ Reruns.find({ _id: 'welshbiographies' }) ] }); }); ``` -------------------------------- ### Performing Non-Reactive Aggregations Source: https://github.com/robfallows/tunguska-reactive-aggregate/blob/master/README.md Illustrates how to run an aggregation only once, making it non-reactive. This is achieved by setting `noAutomaticObserver: true` and not specifying any custom observers, similar to a Meteor Method returning results to a Minimongo collection. The publication will not re-run on subsequent data changes. ```JavaScript Meteor.publish("biographiesByWelshAuthors", function () { ReactiveAggregate(this, Authors, [{ $lookup: { from: "books", localField: "_id", foreignField: "author_id", as: "author_books" } }], { noAutomaticObserver: true }); }); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.