简体   繁体   中英

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.

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}}

   ]
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM