consider I have collection of user_events as follow :
[
  {
      "_id" : ObjectId("5be99dd1da8d4a596423a2d2"),
      "user_id" : 203302,
      "action" : "subscribe",
      "created_at" : ISODate("2018-11-12T15:35:45.000Z"),
  },
  {
      "_id" : ObjectId("5be99dd1da8d4a596423a2d2"),
      "user_id" : 203302,
      "action" : "unsubscribe",
      "created_at" : ISODate("2018-10-12T15:35:45.000Z"),
  },
  ...
]
I need to find users who were subscribed to our service for at least n days. each user can subscribe and unsubscribe for unlimited number of times. for example It is possible for user A to subscribe 100 times and unsubscribe 100 times.
as you can see my documents have a field called action. so in other words, I need to find records for a user with at least n day date interval.
so my result would be sth like this :
[
  {
    "user_id": 2,
    "max_subscription_days": 2
  },
  {
    "user_id(user A)": 5,
    "max_subscription_days": 3
  },
    {
    "user_id": 11,
    "max_subscription_days": 3
  }
]
but I need users who were part of service at least n days. consider user A subscribe to my service and after 3 days unsubscribe.
and next time user A subscribe to my service again and unsubscribe after 5 days. so for this user max subscription days is 5 .
my stack : mongodb : 4.0.0
php : 7.2
 
     
     
    