0

I need to find only that data, Availability date is greater than my current date. where do I put $gte

function getBookingList(){
    return new Promise(function(resolve,reject){
        Booking.find(
            {userId: req.userId}
        ).populate({
            path:'expId',
            model:Experiences
        }).populate({
            path:'slotId',
            model:Availability
        }).exec(function(err,result){
            if(err){
                reject(err);
            }if(result){
                resolve(result);
            }else{
                resolve({"msg":"Booking Experiences not found."});
            }
        });
    });
}

I find the today date to compare.

 var today = new Date(new Date().setHours(0,0,0,0));;
 var todaynew = today.toISOString();

and I know by using

date: {$gte: todaynew}

above I can get it but where do I put in my query?

Cœur
  • 37,241
  • 25
  • 195
  • 267
dev dev
  • 121
  • 1
  • 2
  • 17
  • You likely really mean `.setUTCDate()` there. And unless you did something horribly wrong, then you also don't want it to convert to ISO string, as a `Date` object "should" be what is really expected. And surely then it's just `.find({ userId: req.userId, date: {$gte: today } })`. But you don't even tell us where the date is in your data! Also wrapping in `Promise` is not required. All mongoose methods return a `Promise` already. Simply call `.exec()` without putting a callback in there and the `Promise` is returned. – Neil Lunn Apr 17 '18 at 08:06
  • all working fine only need to compare with Availability collection date which comes in populate – dev dev Apr 17 '18 at 08:09
  • [Querying after populate in Mongoose](https://stackoverflow.com/questions/11303294/querying-after-populate-in-mongoose) – Neil Lunn Apr 17 '18 at 08:10

1 Answers1

1

Your query should be like this:

function getBookingList(){
    return new Promise(function(resolve,reject){
       var today = new Date(new Date().setHours(0,0,0,0));;
       var todaynew = today.toISOString();
        Booking.find(
            {userId: req.userId}
        ).populate({
            path:'expId',
            model:Experiences
        }).populate({
            path:'slotId',
            match: { date: { $gte: new date() || todaynew }}
            model:Availability
        }).exec(function(err,result){
            if(err){
                reject(err);
            }if(result){
                resolve(result);
            }else{
                resolve({"msg":"Booking Experiences not found."});
            }
        });
    });
}
Neel Rathod
  • 2,013
  • 12
  • 28