Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

2 x criteria for Match showing no results

I am trying to show results where the sum of records is greater or equal to 4 and the status matches a string. If I leave off the status field it works fine but adding it in always gives me an empty array even when there should be data.

const bookings = await Booking.aggregate([
    {
        $group: {
            _id: {
                $dateToString: {
                    format: "%Y/%m/%d",
                    date: "$bookingDate",
                },
            },
            totalBookings: {
                $sum: 1,
            },
        },
  },
    {
        $match: {
            totalBookings: {
                $gte: 4,
            },
            status: "Accepted",
        },
  },
]);

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Each booking will have it’s own status. So you need to add that as part of $group

  {
    $group: {
      _id: {
        status: "$status",
        "d": {
          $dateToString: {
            format: "%Y/%m/%d",
            date: "$bookingDate",
            
          }
        },
        
      },
      totalBookings: {
        $sum: 1,
        
      },
      
    }
  }

Then you need to change your match as below

 $match: {
            totalBookings: {
                $gte: 4,
            },
            "_id.status": "Accepted",
        }

It will give you all the Accepted Booking on the given BookinDate which is >= 4.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading