OHLC Aggregation Not working as expected for even numbered time frames

Hello,
I have market tick data in mongodb which I want to resample and create the ohlc data set.
For doing so I am using aggregation pipeline but the pipeline is not working as expected for even minutes timeframe.

  1. My Every day data Starts at 9:15:00 and ends at 15:30:00
  2. when I use 5 minute as time frame it gives me the right result as below
    open_timestamp : 9:15:00, close_timestamp: 9:24:59
    open_timestamp : 9:25:00, close_timestamp: 9:29:59
    and so on.
  3. But when I use 2 minutes as a time frame the pipeline gives me wrong results as below
    open_timestamp : 9:15:00, close_timestamp: 9:15:59
    open_timestamp: 9:16:00, close_timestamp: 9:17:59
    open_timestamp: 9:18:00, close_timestamp: 9:19:59
    and so on,

However the right result should be as below
open_timestamp : 9:15:00, close_timestamp: 9:16:59
open_timestamp: 9:17:00, close_timestamp: 9:18:59
open_timestamp: 9:19:00, close_timestamp: 9:20:59
and so on.

The Pipeline I am using is as below

</>
time_frame_minutes = 2
time_frame_ms = ( time_frame_minutes * 60 * 1000 )

pipeline = [
 {
     "$addFields": {
         "adjusted_timestamp": {
             "$cond": {
                 "if": {
                     "$and": [
                         {"$gte": ["$exchange_timestamp", {"$dateFromParts": {
                             "year": {"$year": "$exchange_timestamp"},
                             "month": {"$month": "$exchange_timestamp"},
                             "day": {"$dayOfMonth": "$exchange_timestamp"},
                             "hour": 9,
                             "minute": 15,
                             "second": 0,
                             "millisecond": 0
                         }}]},
                         {"$lte": ["$exchange_timestamp", {"$dateFromParts": {
                             "year": {"$year": "$exchange_timestamp"},
                             "month": {"$month": "$exchange_timestamp"},
                             "day": {"$dayOfMonth": "$exchange_timestamp"},
                             "hour": 15,
                             "minute": 30,
                             "second": 0,
                             "millisecond": 0
                         }}]}
                     ]
                 },
                 "then": {
                     "$subtract": [
                         {"$toLong": "$exchange_timestamp"},
                         {"$mod": [{"$toLong": "$exchange_timestamp"}, time_frame_ms]}
                     ]
                 },
                 "else": "$$REMOVE"
             }
         }
     }
 },
 {
     "$match": {
         "adjusted_timestamp": {"$exists": True}
     }
 },
 {
     "$group": {
         "_id": "$adjusted_timestamp",
         "open": {"$first": "$last_price"},
         "high": {"$max": "$last_price"},
         "low": {"$min": "$last_price"},
         "close": {"$last": "$last_price"},
         "open_timestamp": {"$first": "$exchange_timestamp"},
         "close_timestamp": {"$last": "$exchange_timestamp"}
     }
 },
 {
     "$sort": {"_id": 1}
 },
 {
    "$unset": "_id"  # Remove the _id field
}]
</>

Kindly help me to sort this issue, I know this has to do something with how the $mod is working here.

  1. How can I make this pipeline work for even minute timeframe like 2 minutes or 10 minutes.

  2. There is another problem i am facing that is when i change my timeframe to longint and then change it back to datetime it gives me result adding +5:30 with my original timestamp.
    I have checked the format of my timestamp it is normal without any timezone in it.

Does mongo give long int of timestamp by adding local lizezone automatically?

The First numbered issue is the priority to be solved so I can move forward with my development.

Please let me know how can I do the point number 1.