Home > Mobile >  How can I get data other than 24 Hours in Mongoose
How can I get data other than 24 Hours in Mongoose

Time:05-27

  var allData = await Account.find({})
  var hoursData = await Account.find({"_time":{ $gt:new Date(Date.now() - 24*60*60 * 1000) }})


  var AllData_array = [];
  allData.forEach(data => {
    var _WalletAdress = data._WalletAdress;
    AllData_array.push(_WalletAdress)
});

var HoursData_array = [];
hoursData.forEach(data => {
  var _WalletAdress = data._WalletAdress;
  HoursData_array.push(_WalletAdress)
});


var test = AllData_array.filter(x => !HoursData_array.includes(x));

I need to fetch any data that is not updated within 24 hours..I made an example like above but this query is running very slow.My Schema is like this;

_ts:{type:String,default:Date.now()}

CodePudding user response:

This might help

Account.find({ 
   "_time" : { 
      $lt: new Date(), 
      $gte: new Date(new Date().setDate(new Date().getDate()-1))
   }   
})

CodePudding user response:

You can simply get data before specific day with this query

let todayDate = new Date();
let yesterdayDate = new Date( todayDate.valueOf() - ( 1000 * 60 * 60 * 24 ) );
let yesterdayDateISO = yesterdayDate.toISOString();

now pass the yesterday Date in ISO format

{_time: { "$lte":  new ISODate(yesterdayDateISO) } }
  • Related