Buscar..


Observaciones

Agregación de servidores

Consultas de agregación promedio en Meteor

¿Es posible empaquetar una biblioteca mongodb 'real' para usar en el lado del * servidor * solo en meteor 0.6?

Agregación de clientes (minimongo)

https://github.com/utunga/pocketmeteor/tree/master/packages/mongowrapper

Agregación de servidores

La solución de Andrew Mao. Consultas de agregación promedio en Meteor

Meteor.publish("someAggregation", function (args) {
    var sub = this;
    // This works for Meteor 0.6.5
    var db = MongoInternals.defaultRemoteCollectionDriver().mongo.db;

    // Your arguments to Mongo's aggregation. Make these however you want.
    var pipeline = [
        { $match: doSomethingWith(args) },
        { $group: {
            _id: whatWeAreGroupingWith(args),
            count: { $sum: 1 }
        }}
    ];

    db.collection("server_collection_name").aggregate(        
        pipeline,
        // Need to wrap the callback so it gets called in a Fiber.
        Meteor.bindEnvironment(
            function(err, result) {
                // Add each of the results to the subscription.
                _.each(result, function(e) {
                    // Generate a random disposable id for aggregated documents
                    sub.added("client_collection_name", Random.id(), {
                        key: e._id.somethingOfInterest,                        
                        count: e.count
                    });
                });
                sub.ready();
            },
            function(error) {
                Meteor._debug( "Error doing aggregation: " + error);
            }
        )
    );
});

Agregación en un método de servidor

Otra forma de hacer agregaciones es mediante el uso de Mongo.Collection#rawCollection()

Esto solo se puede ejecutar en el servidor.

Aquí hay un ejemplo que puedes usar en Meteor 1.3 y superior:

Meteor.methods({
   'aggregateUsers'(someId) {
      const collection = MyCollection.rawCollection()
      const aggregate = Meteor.wrapAsync(collection.aggregate, collection)

      const match = { age: { $gte: 25 } }
      const group = { _id:'$age', totalUsers: { $sum: 1 } }

      const results = aggregate([
         { $match: match },
         { $group: group }
      ])

      return results
   }
})


Modified text is an extract of the original Stack Overflow Documentation
Licenciado bajo CC BY-SA 3.0
No afiliado a Stack Overflow