mongoose 如何对预订集合进行排序

qoefvg9y  于 2023-02-13  发布在  Go
关注(0)|答案(1)|浏览(158)

模型如下所示:

{
        time: String,
        date: Date,
        user_id: String
    },

返回端点获取用户预约是这样的

"reservations": [
        {
            "_id": "63e3bda0a2b3be512b44ba51",
            "time": "10:40",
            "date": "2023-02-06T00:00:00.000Z",
            "user_id": "63d4657708205fda2de700f1"
        },
        {
            "_id": "63e3bda2a2b3be512b44ba56",
            "time": "11:00",
            "date": "2023-02-06T00:00:00.000Z",
            "user_id": "63d4657708205fda2de700f1"
        },
        {
            "_id": "63e3c22bf83f5954d210eb56",
            "time": "13:00",
            "date": "2023-02-06T00:00:00.000Z",
            "user_id": "63d4657708205fda2de700f1"
        },
        {
            "_id": "63e3c224f83f5954d210eb48",
            "time": "10:00",
            "date": "2023-02-10T00:00:00.000Z",
            "user_id": "63d4657708205fda2de700f1"
        },
        {
            "_id": "63e3c228f83f5954d210eb4f",
            "time": "10:00",
            "date": "2023-02-16T00:00:00.000Z",
            "user_id": "63d4657708205fda2de700f1"
        }
    ]

我该如何根据日期和时间对这些预订进行排序。所以最早的日期和最早的时间段,这在所有日期都完成了。

Mongoose 能做到这一点吗?
我尝试过的:

const dates = reservations.map((res) => res.date.toISOString());

    function onlyUnique(value: any, index: any, self: string | any[]) {
        return self.indexOf(value) === index;
    }

    var distinctDates = dates.filter(onlyUnique);

首先获取所有唯一日期

for (let i = 0; i < distinctDates.length; i++) {
        const date = new Date(distinctDates[i]);

        let distinctReservations: { _id: any; time: number; date: Date; user_id: string }[] = [];

        for (let j = 0; j < reservations.length; j++) {
            const reservation = reservations[j];

            if (reservation.date.toISOString() === date.toISOString()) {
                distinctReservations.push({
                    _id: reservation.id,
                    time: hhMMToMinutes(reservation.time),
                    date: reservation.date,
                    user_id: reservation.user_id
                });
            }
        }
        const sortedByMinutes = distinctReservations.sort((a, b) => a.time - b.time);

        sortedReservationsSlotToMinutes.push(sortedByMinutes);
    }

    const flated = sortedReservationsSlotToMinutes.flat(2);
    const sortedReservations = flated.map((res) => {
        let timeSlot = {
            hour: (res.time / 60).toFixed(),
            minute: ''
        };

        const minuteModule = res.time % 60;
        if (minuteModule) {
            timeSlot.minute = minuteModule.toString();
        } else {
            timeSlot.minute = '00';
        }

        return {
            _id: res._id,
            time: timeSlot.hour + ':' + timeSlot.minute,
            date: res.date,
            user_id: res.user_id
        };
    });
    return sortedReservations;

然后循环遍历所有唯一的日期并找到匹配的预订。预订是这个函数中的一个参数。然后将它们的时间转换为分钟,根据最小值排序,然后将这些预订推到一个数组中。之后,我将所有按分钟排序的预订推到一个外部数组中,然后将该数组展平并返回。
然后,我将分钟转换回字符串。

pu3pd22g

pu3pd22g1#

可以使用$dateFromParts构造日期时间,并使用该日期时间进行排序。

db.reservations.aggregate([
  {
    "$addFields": {
      "dateTime": {
        "$dateFromParts": {
          "year": {
            $year: "$date"
          },
          "month": {
            $month: "$date"
          },
          "day": {
            $year: "$date"
          },
          "hour": {
            $toInt: {
              "$substrCP": [
                "$time",
                0,
                2
              ]
            }
          },
          "minute": {
            $toInt: {
              "$substrCP": [
                "$time",
                3,
                2
              ]
            }
          }
        }
      }
    }
  },
  {
    $sort: {
      dateTime: 1
    }
  }
])

Mongo Playground

相关问题