Console.log Raw collection on server

I am trying to learn using Meteor raw Collections with aggregate and grouping on server.

I’ve been testing the results with Meteor Shell running queries and results are just printed out…

This is the query:
db.tariff_types.aggregate([{$match:{Package:false}},{$group: {_id : "$RoomId", HotelId : { $first: "$HotelId" }, MinPrice: {$min: "$Price" }}},{$sort:{HotelId:1}}]);

Now I am moving to the actual application and trying to see results with console.log on localhost server but I can’t get the same results. This is what I am using:

 var TariffTypesRaw = TariffTypes.rawCollection();
 var TariffsInChannelRaw = TariffTypesRaw.aggregate([{$match:{Package:false}},{$group: {_id : "$RoomId", HotelId : { $first: "$HotelId" }, MinPrice: {$min: "$Price" }}},{$sort:{RoomId:1}}]);
 console.log("TariffsInChannelRaw",TariffsInChannelRaw);

However it does not print result. Instead it print something I don’t understand:

    I20180602-10:56:39.645(-3)? TariffsInChannelRaw Readable {
    I20180602-10:56:39.654(-3)?   pool: null,
    I20180602-10:56:39.658(-3)?   server: null,
    I20180602-10:56:39.659(-3)?   disconnectHandler: 
    I20180602-10:56:39.660(-3)?    { s: { storedOps: [], storeOptions: [Object], topology: [Object] },
    I20180602-10:56:39.661(-3)?      length: [Getter] },
    I20180602-10:56:39.662(-3)?   bson: {},
    I20180602-10:56:39.662(-3)?   ns: 'meteor.tariff_types',
    I20180602-10:56:39.663(-3)?   cmd: 
    I20180602-10:56:39.664(-3)?    { aggregate: 'tariff_types',
    I20180602-10:56:39.666(-3)?      pipeline: [ [Object], [Object], [Object] ],
    I20180602-10:56:39.669(-3)?      cursor: { batchSize: 1000 } },
    I20180602-10:56:39.670(-3)?   options: 
    I20180602-10:56:39.672(-3)?    { readPreference: { preference: 'primary', tags: undefined, options: [Object] },
    I20180602-10:56:39.673(-3)?      promiseLibrary: 
    I20180602-10:56:39.674(-3)?       { [Function: Promise]
    I20180602-10:56:39.675(-3)?         Fiber: [Object],
    .....

How do I check results on server?

rawCollection methods, when used without a callback returns a Promise. You will either have to use a Promise chain, or use.async / await syntax.

1 Like

Many Thanks @robfallows !!! You nailed it!

After a bit more of research I found some RawCollection examples and recommendation to use them with Meteor.wrapAsync.

I got it now working with the following code:

     var TariffTypesRaw = TariffTypes.rawCollection();
     var aggregateQuery = Meteor.wrapAsync(TariffTypesRaw.aggregate, TariffTypesRaw);
     var pipeline = [
        {$match:{Package:false}},
        {$group: {_id : "$RoomId", HotelId : { $first: "$HotelId" }, MinPrice: {$min: "$Price" }}},
       {$sort:{RoomId:1}}
    ];
     var result = aggregateQuery(pipeline);

     console.log("result",result);

Glad you got it working. When I mentioned async/await I wasn’t referring to Meteor.wrapAsync (which is equally valid, if a bit harder to “just use”).

My reactive aggregation package, uses async/await, if you want to check the code.