Meteor + MongoDB: check if the date is in the range

It's hard for me to show my records from MongoDB. Basically, I have the leaves_start and leaves_end fields in my MongoDB. These fields have a range of user vacation dates. See the example below.

username: junel

leaves_start: 05/10/2015

leaves_end: 10/10/2015

I want to get all the records in my MongoDB if the current date (e.g. 07/10/2015) is within the range of records_start and leaves_end.

I already tried $ gte and $ lte, but I'm a little confused about how to implement it in its current state.

Here is my example method:

getTowerLeaveData_LV: function(dateToday,tower) {
    var arr = LeavesCollection.find($or: [ 
        { leaves_start: { $lte: dateToday } }, 
        { leaves_end: { $gte: dateToday } } ], 
        leaves_approval_status: {$ne: 'Rejected'}}).fetch();

        return arr
},

Here is my Mongodb Record sample

_____________________________________
name   |  leaves_start   | leaves_end
_____________________________________
Junel  | 10/01/2015      | 10/03/2015
_____________________________________
Jaycee | 10/03/2015      | 10/03/2015
_____________________________________
Tori   | 10/05/2015      | 10/10/2015
_____________________________________
Ryan   | 10/02/2015      | 10/05/2015

If the dateToday value is 03/10/2015, then the method should return Junel, Jaycee, and Ryan records.

I hope this makes sense. Thank you, guys!

+4
3

:

MyCollection.find({
  leaves_start: { $lte: new Date },
  leaves_end:   { $gte: new Date }
});
+1
startDate =  ;// get Start Date from UI Convert it to date format using new Date();
endDate   =  ;// get End Date from UI Convert it to date format using new Date();
MyCollection.find({
  leaves_start: { $lte: endDate},   // start Less and endDate
  leaves_end:   { $gte: startDate } // end greater than StartDate
});

startDate endDate , , .

+1

I'm not sure if this will be useful, but here is the code I came up with:

RECORDS enter image description here

Method

//monthyear = "10-2015"
//numOfDays = 31

getTowerLeaveData_LV: function(monthyear, numOfDays,tower, userid, username) {
        var selectedMonthYear = monthyear.split("-");
        var tempArr = new Array();
        var reArr = new Array()

        tempArr.push(username)
        reArr.push(username);

        LeavesCollection.find({associate_tower: {$in: tower}, leaves_approval_status: {$ne: 'Rejected'}, user_id: userid},{sort:{leaves_timestamp   :-1}},{fields: {_id:1,user_id:1,associate_id:1, associate_fullname:1,leaves_type:1,leaves_start:1,leaves_end:1, leaves_days:1}}).forEach(
          function(leaver) {
              for(var a=1; a!=numOfDays+1; a++) {
                var dateActive = selectedMonthYear[0] + "/" + a.toString() + "/" + selectedMonthYear[1];
                var res = dateCheck(leaver.leaves_start, leaver.leaves_end,dateActive);
                if(res == true) {
                    tempArr.splice(a, 0,[leaver.leaves_approval_status,leaver.leaves_type,leaver._id,leaver.associate_fullname,a]);
                }
              }
          });

        for(var a=1; a!=numOfDays+1; a++) {
          var temp = findKey(tempArr,a);

          if(temp != false) {
            reArr.push(tempArr[temp]);
          } else {
            reArr.push('null')
          }
        }

        return reArr;

    },

FUNCTIONS MISC JS:

function dateCheck(from,to,check) {

    var fDate,lDate,cDate;
    fDate = Date.parse(from);
    lDate = Date.parse(to);
    cDate = Date.parse(check);

    if((cDate <= lDate && cDate >= fDate)) {
        return true;
    }
    return false;
}



function findKey(array, search) {
  var theIndex = false;
  for (var i = 0; i < array.length; i++) {
    if (array[i].indexOf(search) > -1) {
        theIndex = i;
        break;
    }
  }
  return(theIndex);
}

OUTPUT IN ARRAY: enter image description here

EXPLANATION OF EXIT:

Elements after the name in the array are equal to numOfDays (these are dates). If the program finds a match date in the range between "leaves_start" and "leaves_end", it will return the array data from mongodb, if not, it will return "null".

0
source

Source: https://habr.com/ru/post/1613225/


All Articles