Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose query specific date range and also get null values

I have a situation where i need to get the dates between specific dates and also get the dates which are not thr (meaning which are empty)

Below is the way of query that am presuming to work like, please help me get corrected.

EvaluationTraining.find({
    mnguser: userid,
    evalDate: "", //i want the empty ones too
    evalDate: {
        '$gte': firstDay,
        '$lte': lastDay
    }
    })
    .sort({evalDate: 'desc'})
    .then(function(eval){
      //results
    })
    .catch(function(err){
      //err
    })

Thanks in advance

like image 823
foxenn_pro Avatar asked Mar 22 '26 13:03

foxenn_pro


2 Answers

try this

EvaluationTraining.find({
    mnguser: userid,
    $or: [{
        evalDate: ""
    }, {
       evalDate:{ 
        '$gte': firstDay,
        '$lte': lastDay
       }
    }]
})
like image 167
Rahul Sharma Avatar answered Mar 24 '26 02:03

Rahul Sharma


In case we need to find fields which is not present in a few documents instead of empty data, we can use $exists

EvaluationTraining.find({
    mnguser: userid,
    $or: [{
        evalDate: { $exists: false }
    }, {
       evalDate:{ 
        '$gte': firstDay,
        '$lte': lastDay
       }
    }]
})
like image 33
Akhilesh krishnan Avatar answered Mar 24 '26 03:03

Akhilesh krishnan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!