David Geismar
David Geismar

Reputation: 3422

Query with waterline and sails JS

I am using SailsJS with waterline. My user model has_many bookings. I need to get all users that have passed more than 3 bookings in the last months. I managed to write a query that gets all users that made a booking in the last month.

findUsersWhoBookedLastMonth: function(cb){
    var dateM30J = moment().subtract(30,'days').format();
    console.log(dateM30J);
    Reservation.find({
      select: ['user'],
      where:{
        date:{
          '>=' : dateM30J
        }
      },
    }, function(err, user_ids) {
        if (err) {
          console.log('ERROR: ' + err);
          cb(err, null)
        }
        else {
          user_ids = _.uniq(user_ids);
          cb(null, user_ids)
        }
      });

  },

but i can't figure out how to get all users that have booked more than three times in the last month

Upvotes: 1

Views: 190

Answers (1)

Miguel Cunha
Miguel Cunha

Reputation: 663

Your Reservation.find(...) returns an array, just check for each user if the array size is > 3.

Or you can use .count(), it has the same effect.

Upvotes: 1

Related Questions