Search code examples
mongodbmongoosemeteormongodb-query

I want to query on-going meeting between the duration defined in collection based on occurrence time


Facing issue to on-going meeting when meeting started it should show meeting until duration present in collection.

This is collection of meeting with occurence time of meeting

{
        "status": "ACTIVE",
        "id": 7,
        "uid": 172,
        "title": "meeting with 2 persons",
        "description": "meeting description 3",
        "duration": 3600,
        "participants": [{
            "role": "ORGANIZER",
            "status": "ACCEPTED",
            "uid": 172
        }, {
            "role": "ATTENDEE",
            "status": "PENDING",
            "uid": 173
        }, {
            "role": "ATTENDEE",
            "status": "PENDING",
            "uid": 175
        }],
        "created": {
            "$date": "2019-09-01T12:01:32.000Z"
        },
        "occurrence_time": {
            "$date": "2019-09-02T13:11:38.000Z"
        },
        "created_at": 1605187219,
    }

What I have tried

Meetings.find({ 
    'participants.uid': 172,
    status: 'ACTIVE',
    occurrence_time: {
      $gte: new Date(moment().subtract('$duration', 'seconds'))
    }
  }, { 
    sort: { occurrence_time: 1 }
  }).fetch();

Solution

  • You can use $where to use a Javascript function during the evaluation of your query:

    Meetings.find({$where: function() {
        return Date.now() < (new Date(this.occurrence_time.$date)).getTime() + this.duration;
      }
    })
    

    By the way, if you want to use aggregation in Meteor, you can just access the raw collection like this:

    Meetings.rawCollection().aggregate([
       // .. aggregation pipeline ..
    ]);