Searching for entries between two dates in MongoDB - mongodb

I have a collection "received" in which documents contain a embedded filed which consists of year and week details. For example:
"YearANDWeek" : {
"Year" : 1908,
"Week" : 6
}
If I want to get data between year 1956 and week 23 to year 2000 and week 40 what query I need to use? I can use $gte and $lt but there is conflict between weeks.

I would use the following query.
db.received.find({$where:"((this.YearANDWeek.Year > 1956 && this.YearANDWeek.Year < 2000) || (this.YearANDWeek.Year == 1956 && this.YearANDWeek.Week >= 23) || (this.YearANDWeek.Year == 2000 && this.YearANDWeek.Week <= 40))"});

I this you can try this
db.recieved.find({$where:"((this.YearANDWeek.Year >= 1956 && this.YearANDWeek.Week >= 6) || (this.YearANDWeek.Year < 2000 && this.YearANDWeek.Year > 2000 && this.YearANDWeek.Week < 40)) && (this.YearANDWeek.Year >= 1956 && this.YearANDWeek.Year < 2000)"})

Related

Flag rows in a SAS table dated from the previous 30 days

I have a dataset that uses dates, and I want to create two variables. One is to flag where the 'created_date' is from the last 30 days, and another to flag where the 'created_date' is from the previous 30 days. The 30 days flag works fine, but the I'm having issues with the previous 30 days. Here is my code:
if created_date >= intnx("day","&sysdate"d,-30,"same") then Flag_30D = 1; else Flag_30D = 0;
if created_date <= intnx("day","&sysdate"d,-30,"same") and created_date >= ("day","&sysdate"d,-60,"same") then Flag_P30D = 1; else Flag_P30D = 0;
You have a typo and left out INTNX in second part of if expression.
The question/answer will likely be closed/deleted by a moderator.
created_date >= ("day","&sysdate"d,-60,"same")
should be
created_date >= INTNX("day","&sysdate"d,-60,"same")
^^^^^

How to add 2 hours to a date field

I'm working with DB2 database in which the time field is the following string:
12104102000000
where it means, from the left:
> 1 --> is the century 21 --> is the year 04 --> is the month 10 --> are
> the hours 20 --> are the minutes 00 --> are the seconds 000 --> are
> the milliseconds
Well, the problem is that this field is synchronized with the reference time, so respect to Italy (where I'm working) is two hours back.
I have a query where usually I extract the date field with substr function and the starting part is:
select substr("Message_Time",4,2) || '/' || substr("Message_Time",6,2) || '/' || '20' || substr("Message_Time",2,2) as "Date",
substr("Message_Time",8,2) || ':' || substr("Message_Time",10,2) as "Hour"
I need to add to hour substr only, the 2 hour to bring the date to Italy.
But I think the issue is at the change of the day, because if I have e.g. 20th of April, 22:40, it corrisponds to 21st of April, 00:40 in Italy.
How should I do in order to make it working?
Please tag which DB2 you use.
If DB2LUW or DB2 for I, you can use
to_date('12104102000000', 'YYYMMDDHH24MISSFF1') - 100 YEARS + CURRENT TIMEZONE
I guess it works with DB2 for Z but I'm not sure.
Try this, if you want your result to be in the same string form:
SELECT
SUBSTR (D, 1, 1)
|| SUBSTR
(
TO_CHAR
(
TO_DATE(CAST(19 + SUBSTR(D, 1, 1) AS CHAR(2)) || SUBSTR(D, 2), 'YYYYMMDDHH24MISSFF3') + 2 HOURS
, 'YYYYMMDDHH24MISSFF3'
)
, 3
)
FROM (VALUES '12104102240000') T (D)

How to query mongoDB for a given date range?

I am using mongoose to query data for my node.js application for a given month where I accept the input for a year and month from the user.
This is what I am doing
var lastDay = 31
if (month == 3 || month == 5 || month == 8 || month == 10) lastDay = 30
else if (month == 1) lastDay = 28
var cookies = await Cookies.find({
docDate: {
"$gte": new Date(parseInt(year), parseInt(month), 1),
"$lte": new Date(parseInt(year), parseInt(month), lastDay)}}).sort("docDate");
When I try to query the db for the month of february, I am getting results only uptil the 26 Feb and the result also includes data from january.
Please help me figure out what I am doing wrong.
I think the issue is with the date that is passed to the query.
new Date(2021,01,01); --> 2021-01-31T16:00:00.000Z
new Date(Date.UTC(2021,01,01)); --> 2021-02-01T00:00:00.000Z
If the date field in the database is in UTC then try using new Date(Date.UTC(year, month, day))
Use moment.js library:
docDate: {
"$gte": moment().startOf('month').toDate(),
"$lte": moment().endOf('month').toDate()
}
moment() is the current time. In order to input specific day, use moment(year + '-' + month, "YYYY-MM") for example.

query date that is modified in document

How can I make a query on mongodb that says: currentTime <= endDate + 7 days or endDate + 7 days >= currentTime?
If I do something like this endDate: { $lte: (current + 7 days) } (ignore the date calculation) it will grab all records from beginning that the endDate less than 7 days from now.
Basically I want to find records that is not ended yet after 7 days from the end date.
try this "endDate" : { "$lte" : new Date(ISODate().getTime() - 1000 * 3600 * 24 * 7) }

LINQ query on timestamp between 22:30 and 1:00

I am trying to use LINQ to get a list of entities where the timestamp falls between either 22:30 - 24:00 or 00:00 - 1:00 on a saturday or sunday and I am looking to set both timeframes in the one LINQ query but I have been unable to do so and have had to break it up as follows:
weekdayNight = intervalInformations.Where(ii => ii.IntervalPeriodTimestamp.TimeOfDay >= new TimeSpan(22, 30, 0)
&& ii.IntervalPeriodTimestamp.TimeOfDay <= new TimeSpan(23, 30, 0)
&& ii.IntervalPeriodTimestamp.DayOfWeek != DayOfWeek.Saturday
&& ii.IntervalPeriodTimestamp.DayOfWeek != DayOfWeek.Sunday
&& !dates.Contains(ii.IntervalPeriodTimestamp.ToShortDateString()))
.OrderBy(ii => ii.IntervalPeriodTimestamp)
.ToList();
weekdayNight2 = intervalInformations.Where(ii => ii.IntervalPeriodTimestamp.TimeOfDay >= new TimeSpan(0, 0, 0)
&& ii.IntervalPeriodTimestamp.TimeOfDay <= new TimeSpan(0, 30, 0)
&& ii.IntervalPeriodTimestamp.DayOfWeek != DayOfWeek.Saturday
&& ii.IntervalPeriodTimestamp.DayOfWeek != DayOfWeek.Sunday
&& !dates.Contains(ii.IntervalPeriodTimestamp.ToShortDateString()))
.OrderBy(ii => ii.IntervalPeriodTimestamp)
.ToList();
weekdayNight.Add(weekdayNight2);
How can I write this query without needing to join two different queries
Adding an OR between the two conditions defining time intervals should do the trick:
weekdayNight = intervalInformations.Where(ii =>
((ii.IntervalPeriodTimestamp.TimeOfDay >= new TimeSpan(22, 30, 0) && ii.IntervalPeriodTimestamp.TimeOfDay <= new TimeSpan(23, 30, 0))
|| (ii.IntervalPeriodTimestamp.TimeOfDay >= new TimeSpan(0, 0, 0) && ii.IntervalPeriodTimestamp.TimeOfDay <= new TimeSpan(0, 30, 0)))
&& ii.IntervalPeriodTimestamp.DayOfWeek != DayOfWeek.Saturday
&& ii.IntervalPeriodTimestamp.DayOfWeek != DayOfWeek.Sunday
&& !dates.Contains(ii.IntervalPeriodTimestamp.ToShortDateString())
)
.OrderBy(ii => ii.IntervalPeriodTimestamp)
.ToList();
Note: Your query appears to exclude the last half-hour between 23:30 and midnight. If this is not intentional, you can simplify the query even further:
weekdayNight = intervalInformations.Where(ii =>
(ii.IntervalPeriodTimestamp.TimeOfDay >= new TimeSpan(22, 30, 0)
|| ii.IntervalPeriodTimestamp.TimeOfDay <= new TimeSpan(0, 30, 0))
&& ii.IntervalPeriodTimestamp.DayOfWeek != DayOfWeek.Saturday
&& ii.IntervalPeriodTimestamp.DayOfWeek != DayOfWeek.Sunday
&& !dates.Contains(ii.IntervalPeriodTimestamp.ToShortDateString())
)
.OrderBy(ii => ii.IntervalPeriodTimestamp)
.ToList();