query date between

 avatar
unknown
javascript
3 years ago
1.8 kB
5
Indexable
const firstDate = "2021-01-10";
        const lastDate = "2021-01-20";

        const doc = await AppointmentModel.aggregate([
          {
            $match: {
              $and: [
                { doctor_id: new Types.ObjectId(id) },
                { status: "Paid" },
                {
                  consultation_date: {
                    $gte: new Date(firstDate),
                    $lt: new Date(lastDate),
                  },
                },
              ],
            },
          },
          {
            $lookup: {
              from: "users", //or Races.collection.name
              localField: "user_id",
              foreignField: "_id",
              as: "user",
            },
          },
          {
            $lookup: {
              from: "doctor", //or Races.collection.name
              localField: "doctor_id",
              foreignField: "_id",
              as: "doctor",
            },
          },
          {
            $sort: {
              created_at: -1,
            },
          },
          {
            $project: {
              _id: 1,
              billed: 1,
              consultation_date: 1,
              consultation_hours: 1,
              doctor_id: 1,
              doctor_name: 1,
              online_consultation_price: 1,
              patient_name: 1,
              payment_proof: 1,
              phone_number: 1,
              provider_code: 1,
              queue: 1,
              registration_number: 1,
              status: 1,
              user_id: 1,
              created_at: 1,
              "user.name": 1,
              "doctor.nameDoctor": 1,
              "user.phone": 1,
              "provider.name": 1,
            },
          },
        ]);