0

I tried using mongodb pipeline to aggregate document

but always returning empty list, already tried using naivedatetime and datetime from chrono package but still the same and I always give 1 week for both start time and endtime so UTC conversion is should not fail me

I havent tried using Unix timestamp or bson mongodb date(?) CMIIW

funny part is I tried to query in MongoDBCompass (official app), and still cant find the document

is chrono conversion is failling me or I stored in wrong format ? because tried from String to naivedatetime and DateTime and yet cant find the document from the range

pub struct Clock {
    pub corpo_uid: String,
    pub employee_uid: String,
    pub clock_time: DateTime<Utc>,
    pub status: ClockStatus,
    pub notes: Option<String>,
    pub location: Option<Location>,
    pub break_duration: Option<i64>,
    pub total_duration: Option<i64>,
}

pub async fn get_clocks_by_employee_and_range(
    company_id: &String,
    office_id: &String,
    clock_data: &ClockView,
    office_collection: &Collection<Office>,
    clock_collection: &Collection<Clock>,
) -> Result<Vec<Clock>, ResponseError> {
    let tz = match get_office_timezone(company_id, office_id, office_collection).await {
        Ok(tz) => tz,
        Err(e) => return Err(e),
    };

    let start_time = tz
        .from_local_datetime(&clock_data.start_time)
        .unwrap()
        .with_timezone(&Utc);
    let end_time = tz
        .from_local_datetime(&clock_data.end_time)
        .unwrap()
        .with_timezone(&Utc);

    let pipeline = vec![
        doc! {
            "$match": {
                "employee_uid": &clock_data.employee_uid,
                "clock_time": {
                    "$gte": start_time,
                    "$lte": end_time,
                }
            }
        },
        doc! {
            "$sort": {
                "clock_time": 1
            }
        },
    ];

    let options = None;
    let mut result = clock_collection
        .aggregate(pipeline, options)
        .await
        .map_err(|_| ResponseError::Failed)?;

    let mut clock_list: Vec<Clock> = Vec::new();

    while let Some(doc) = result.try_next().await.map_err(|_| ResponseError::Failed)? {
        let clock: Clock =
            bson::from_bson(bson::Bson::Document(doc)).map_err(|_| ResponseError::Failed)?;
        clock_list.push(clock);
    }

    Ok(clock_list)
}
Tony Hart
  • 145
  • 1
  • 12

0 Answers0