Reputation: 69
I'm having a difficulty showing my records from MongoDB. Basically I have some fields 'leaves_start' and 'leaves_end' in my MongoDB. This fields has the date range of the user's leave. See example below.
user_name : junel
leaves_start: 10/05/2015
leaves_end: 10/10/2015
I want to get all the records in my MongoDB if the current date (e.g 10/07/2015) is within the range of the record's leaves_start and leaves_end.
I already tried $gte and $lte but I'm a little bit confused on how to implement it on my current state.
Here's my sample 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's my sample Mongodb Record
_____________________________________
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 value of dateToday is 10/03/2015, then method should return the records of Junel, Jaycee and Ryan.
I hope that this makes sense. Thanks guys!
Upvotes: 4
Views: 582
Reputation: 69
I'm not sure if this will be helpful, but here's the code that I came up with:
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;
},
MISC JS FUNCTIONS:
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);
}
EXPLANATION OF OUTPUT:
The items after the Name in the array is equal to the value of numOfDays(which is dates). If the program find a match date to the range between "leaves_start" and "leaves_end", it will return the array data from mongodb, if not, it will return "null".
Upvotes: 0
Reputation: 1203
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
});
if the startDate and endDate is same you get all the records for that date , else it will be date Range.
Upvotes: 2
Reputation: 11207
You'd do it like this
MyCollection.find({
leaves_start: { $lte: new Date },
leaves_end: { $gte: new Date }
});
Upvotes: 1