Sejista Carl
Sejista Carl

Reputation: 101

mongodb find the records which is not on the specific date

I don't find a way to fetch the records which are not created on a specific date. Date is in iso format, but I just need to compare the date part excluding the time.

like where rec_date != '2018-08-02' 

I am already having a hard time to get the records of the specific date, but I managed it somehow using the following

    var param_date = '2018-08-23T09:47:00Z'

    var day = moment(param_date, "YYYY-MM-DD")
    var pday = moment(param_date, "YYYY-MM-DD").subtract(1, 'day')
    var nday = moment(param_date, "YYYY-MM-DD").add(1, 'day')



     Model.find({
      $and: [

        // match exact date
          {"rec_date": {$gt: day}},
          {"rec_date": {$lt: day2}}


      ]
  },

            function(err,docs) { 
                 res.json(docs)
            } );

but I have no idea how to do it for not equal

$ne doesn't seem to work as it is comparing the whole ISO date, just need to compare the date part.

Upvotes: 0

Views: 48

Answers (1)

Matthew P
Matthew P

Reputation: 735

You can just do the opposite of what you are currently doing when finding an exact date:

Model.find({
   $and: [
      {"rec_date": {$gt: day2}},
      {"rec_date": {$lt: day}}

   ]
}

Upvotes: 1

Related Questions