0

Stack: + Mongoose 4.4.10 (last stable version) already tested with older versions + MongoDb 2.6.3

I execute that function in mongodb console, with successfully re

db.activities.aggregate(
    { $match : { 'organizer': ObjectId("5408e4609640de8768c1d212") } }
  , { $group : 
       { _id : "$organizer",  
        totalSwims: { $sum : 1 }, 
        longestSwim :  { $max: "$distance" }, 
        moreDistanceSwim :  { $max: "$duration" }, 
        warmestWaterSwim :  { $max: "$activityWeather.waterTemperature" }, 
        coldestWaterSwim :  { $min: "$activityWeather.waterTemperature" }, 
        warmestSwim :  { $max: "$activityWeather.temperature" }, 
        coldestSwim :  { $min: "$activityWeather.temperature" }}});

{ "_id" : ObjectId("5408e4609640de8768c1d212"), "totalSwims" : 50, "longestSwim" : 6512.997, "moreDistanceSwim" : "02:35", "warmestWaterSwim" : "22", "coldestWaterSwim" : "22", "warmestSwim" : "15", "coldestSwim" : "15" }

But If I try through mongoose is returning always an empty array [] I already have some aggregate functions working well, but don't know why that one is not working.

 activities.aggregate([
  { $match : { 'organizer': userId } },
  { $group : { 
   _id : "$organizer",  
   totalSwims: { $sum : 1 }, 
   longestSwim :  { $max: "$distance" }, 
   moreDistanceSwim :  { $max: "$duration" }, 
   warmestWaterSwim :  { $max: "$activityWeather.waterTemperature" }, 
   coldestWaterSwim :  { $min: "$activityWeather.waterTemperature" }, 
   warmestSwim :  { $max: "$activityWeather.temperature" }, 
   coldestSwim :  { $min: "$activityWeather.temperature" }
  } } ]
  , function(err, result){
   if (err) {
    console.error('Problem %s', err);
    return next(err);
   } else {
    console.error('Result %j', result);
    return next(null, result);
   }
  });

Any idea?

Thank you

Iván Peralta
  • 851
  • 1
  • 9
  • 25

1 Answers1

2

Sorry, after searching for a while before posting without any relevant result I finally figured it out. It seams that, just for that case, I need to ensure the id with the ObjectId method instead to send just an string.

var ObjectID = require("mongodb").ObjectID;
activities.aggregate([
    { $match : { 'organizer': ObjectID(userId) } },
Iván Peralta
  • 851
  • 1
  • 9
  • 25
  • There's bucketloads of duplicates ( and a great big Issue on GitHub). So it wasn't a very hard search. Also if you intend to self answer then do it at the same time and don't "post first" then "answer later" because that looks like you are asking a "real qusestion". There is a specific option to include your "self answer" when you post. – Blakes Seven Mar 26 '16 at 20:32
  • You also got it wrong since there is no need to pull in a separate dependency. Mongoose is build on top of the mongodb driver. So the dependency is already there, and accessed in a different way. See the correct way in [Moongoose aggregate $match does not match id's](http://stackoverflow.com/questions/36193289/moongoose-aggregate-match-does-not-match-ids) – Blakes Seven Mar 26 '16 at 20:36
  • Thanks for your feedback Blakes, it was no my intention to just spam here, I'll check your feedback asap, i really appreciate it. – Iván Peralta Mar 26 '16 at 20:56
  • Thanks! You are a life saver. – Hiran Walawage Feb 21 '18 at 10:09