Searching Dates and Ignoring time and date in mongoDB node.js - javascript

I am using node.js and monodb database where I want to query records based on date and I want to ignore time and date only month and year will be matched here is my code:-
collection.find({ date: { "$gte": sDate, "$lt": eDate) } }).count(function (e, c) {
});
this is working but matching date and time as well how I can match only month and year? Please help me to solve this problem.
Edit:- some data from collection:-
{
"_id" : ObjectId("5535e76f82a964d011e34fcf"),
"VisitorId" : "5535e72a82a964d011e34fcb",
"date" : ISODate("2015-01-21T06:00:15.761Z"),
}
{
"_id" : ObjectId("5535e75f82a964d011e34fcf"),
"VisitorId" : "5535e72a82a964d011e34fcb",
"date" : ISODate("2015-04-21T06:00:15.761Z"),
}
I will pass two params i.e {month:"1",year:"2015"};
and in output first docs should be display.
Thanks

You could use the $where operator in your query:
collection.find({
"$where": function() {
return this.date.getMonth() == 0 && this.date.getFullYear() == 2015
}
}).count(function (err, data) {
// Handle err
});
However, query performance is rather compromised because using $where alone requires a table scan, it takes a global lock. You should use $where only when you can't express your query using another operator. If you must use $where , try to include at least one other standard query operator to filter the result set.
Other options are to modify your schema and store the month in its own property (if it's a common field in your queries). You are guaranteed better query performance since the field can be indexed.
The other option will be when query a specific month and year, create a query object that only looks for the start and the end of that specific month.
var sDate = new Date(2015, 0, 1);
var eDate = new Date(2015, 1, 1);
collection.find({ date: { "$gte": sDate, "$lt": eDate) } }).count(function (e, c) {
});

Related

There is no calibration between MongoDB UTC time and local time

Data is stored and inquired through the API on the web page, and the API is using MongoDB.
The server space has been unified to UTC time so that the same results can be achieved in different time zones.
MongoDB uses the Mongoose schema as follows:
const userSchema = new Schema({
userId : {
type : String
},
score : {
type : Number
},
createdAt : {
type : Date,
default : Date.now
}
});
Because Date.now in the schema is in the createdAt field by default, it does not pass the Date value separately when querying the create or update of mongoose.
Considering the case where offset exists based on UTC time, the time is calculated using moment.js as follows:
// -540 offset value for KST 9 hours faster than UTC
const utc= moment.utc().add(offset, 'm').format('YYYY-MM-DDTHH:mm:ssZ');
let beginDate = new Date(utc);
let endDate = null;
let year = beginDate.getFullYear();
let month = beginDate.getMonth();
let date = beginDate.getDate();
// To view the full duration of the day
beginDate = new Date(new Date(year, month, date).setHours(0, 0, 0, 0));
endDate = new Date(new Date(year, month, date).setHours(23, 59, 59, 59));
// Find document
const user = await userSchema.aggregate([
{
$unwind : 'lists'
},
{
$match : {
'lists.createdAt' : {
$gte : beginDate,
$lte : endDate
}
}
},
...
]);
For example, if you make a query in Korea, the data inquiry may differ from the time after midnight and to 9 a.m. the next day.
What is wrong with the above parallax correction logic? I don't exactly understand the current problem.
Why so difficult? Simply use
{
$match : {
'lists.createdAt' : {
$gte : moment().startOf('day').toDate(),
$ltr : moment().endOf('day').toDate()
}
}
}
moment().startOf('day').toDate() returns the begin of current day in your local time zone. I live in Switzerland, thus it returns ISODate("2023-01-17T23:00:00.000Z")
But you can specify also a time zone, e.g.
moment.tz('Asia/Seoul').startOf('day').toDate();
ISODate("2023-01-17T15:00:00.000Z")
The problem is moment.utc().add(...) really modifies the time value. But that is not what you need to do, you like to change only the way how the time is displayed in your local time zone.
For comparison and calculations, the displayed value does not matter. All methods are done in UTC time only.

Mongodb aggregation query by date difference

I am trying to make a cron job which syncs my documents. It should try to do it x amount of times but only after 2h have passed since the last try. On each document I have "lastSyncAt" field which I can use.
{
"_id" : ObjectId("628d8c4ddb65027a2cfd1019"),
"calculatedAt" : "",
"count" : 0,
"createdAt" : "2022-05-25 01:54:21",
"lastSyncAt" : "2022-05-25 03:54:21"
}
How should I approach this?
Should I get the lastSyncAt value in pipeline and calculate difference between currentDate? How do I get only the hours portion of that in my pipeline?
Should I convert lastSyncAt into unix and get currentDate in unix substract them and divide by 7200 to see if it is greater than 2?
Or should I take another approach?
I'm not even sure what approach to take. Not looking for code but an idea how to handle this.
Thx
Update:
Thanks to #derek-menénedez I managed to get it working as shown below:
[
// Stage 1
{
$addFields: {
lastSyncAt: {
$dateDiff: {
startDate: {$dateFromString: {
dateString: "$lastSyncAt",
timezone: "Europe/Zagreb"
}},
endDate: "$$NOW",
unit: "minute",
timezone: "Europe/Zagreb"
}
}
}
},
// Stage 2
{
$match: {
lastSyncAt: {
$gt: 120
}
}
}
]
You can use the aggregation framework to achieve the things that you want:
https://mongoplayground.net/p/1RzPCYbeHEP
You can try to remove the projection on the example to validate the number of hours.
$dateFromString operator helps you to create a date from a string
$dateDiff operator helps you to extract the diff of two dates

Using ISODate with mongodb query and node.js

I am creating a string -
queryString - {"Project": "Serenity","DateOfWalkin": {"$gte": "2022-03-01" ,"$lt": "2022-03-31"} }
then parsing it - queryObject = JSON.parse( queryString )
It is parsing successfully, but when I run the query it does not give me any result.
Now when I update the same query by using ISODate for both dates, and then tried running the query in Mongo compass, it works fine.
I want to use ISODate() but the problem is the way I am building the queryString is based on different conditions from the user input, now if a particular condition is true then and only then I want to query that particular field. For other fields I am searching, which are of type "String" in my db, I am able to get the correct result, but for date I am facing this issue.
startDate and endDate are user inputs using date picker in HTML
TRIAL number 1
queryString = queryString + ,"DateOfWalkin": {"$gte": ISODate("${startDate}")} ,"$lt": { "$date" : ISODate("${endDate}") } }
This will give a parser error saying identifier "I" not found, hence I am not able to parse this string.
To combat this, I tried to use new Date object, tried to convert the date using toISOString, but it didn't work.
TRIAL number 2
queryString = queryString + ,"DateOfWalkin": {"$gte": "ISODate("${startDate}")"} ,"$lt": { "$date" : "ISODate("${startDate}")" } }
If we use double quotes for ISODate as well, it will solve the parser issue but Mongo query will not give any result and it is treating ISODate wrapper as a string literal.
TRIAL number 3
queryString = queryString + ,"DateOfWalkin": {"$gte": "${startDate}"} ,"$lt": { "$date" : "${endDate}" } }
Does not give result
TRIAL number 4
Using new Date() to get the ISODate format
Query becomes -
{
Project: 'Serenity',
DateOfWalkin: {
'$gte': 'Tue Mar 01 2022 05:30:00 GMT+0530 (India Standard Time)',
'$lt': 'Thu Mar 31 2022 05:30:00 GMT+0530 (India Standard Time)'
}
}
Still it doesn't give any result
TRIAL number 5
Query object -
{
Project: 'Serenity',
DateOfWalkin: {
'$gte': { '$date': '2022-03-01' },
'$lt': { '$date': '2022-03-01' }
}
}
Above doesn't give result
Is there any way I can store the value of ISODate("${startDate}") and use that variable in the main queryString?
Or is there a better way to compare dates when I am using the input dates as user input?
I have tried multiple approaches and kinda feeling stuck at this point.
Why do you parse the string?
Apart from your current problem, you also open the door for NoSQL-Injection. You can compose the query object also directly like this:
const queryObject = {};
queryObject["Project"] = "Serenity";
queryObject["DateOfWalkin"] = { $gte: ISODate("2022-03-01"), ["$lt"]: ISODate("2022-03-31") };
const ninDates = [];
ninDates.push(ISODate("2022-03-10"));
ninDates.push(ISODate("2022-03-20"));
queryObject["DateOfWalkin"]["$nin"] = ninDates;
db.collection.find(queryObject)
If you really insist to parse the string, then use ejson
EJSON.parse('{ "Project": "Serenity", "DateOfWalkin": { "$gte": { "$date": "2022-03-01" }, "$lt": { "$date": "2022-03-01" } } }')
Should give desired queryObject. Note, keys and values need to be enclosed by double-quotes (")

Multiple SQL query in NodeJS

I have to perform nested queries to return values from the database for a chart. I have to do a total of 12 queries, in which each query adds up the number of customers in each month of the year.
If I use this mode, I get the right result, but written in a horrible way because I do so many lines of code:
sql = "SELECT SUM(customer) as January FROM visit WHERE date >= '2020-01-01 00:00:00' AND date <= '2020-01-31 23:59:59' AND id='4' ";
db.query(sql, function(error, result, result_fields) {
if (error) {
res.render('loginform.ejs', { message: err_message, level: "error" } );
winston.error(error);
} else {
//SAVE DATA
sql = "SELECT SUM(customer) as february FROM visit WHERE date >= '2020-02-01 00:00:00' AND date <= '2020-02-28 23:59:59' AND id='4'";
//FEBRUARY
db.query(sql, function(error, result, result_fields) {
if (error) {
res.render(...);
winston.error(error);
} else {
req.session.february_customers = ((result[0].february == null)) ? 0 : result[0].February;
...
//This is followed by another 10 queries for the other 10 months of the year.
As you can see there are many lines of code !!
Is there any way to optimize? For example using a for loop?
You should to use single query that returns yearly data grouped by month:
SELECT
DATE_FORMAT(date, '%M') AS Month,
SUM(customer) as Visits
FROM visit
WHERE
date BETWEEN '2020-01-01 00:00:00' AND '2020-12-31 23:59:59'
AND id=4
GROUP BY MONTH(date);

Saving a String in MongoDB as a date saves wrong values

I am trying to save a string in MongoDB as a date but having hard times for storing the right values.
In Mongoose schema data value is stored as Date, however, I pass the value to data as new Date("MM-dd-YYYY") but when I look up in the database the value is transformed to this format ISODate("YYYY-MM-dd-1T21:00:00Z")
The format wouldn't bother me if the date would be the same but as you notice the value in the database is one day earlier then the value which I want to be.
So instead of 2018-09-20 is 2018-08-19. My guess is that default UTC time is not the same or something like that but how can I set the correct UTC time?
Edit:
var mongoose = require('mongoose')
var dateformat =require('moment');
//Schema
var ReservationSchema = mongoose.Schema({
name : {
type:String,
required : true,
},
numberOfGuests : {
type : Number ,
required : true,}
,
email: {
type : String,
required:true,
},
phone: {
type : String,
required:true,
},
data:{
type:Date,
require:true,
},
timetables:{
type:String,
require:true,
},
furtherRequests: {
type : String,
}
});
var reservvar = module.exports = mongoose.model('Rezervari', ReservationSchema ,'Rezervari');
module.exports.createReservation = function (query,callback){
//query.data = dateformat.utc(query.data).format("MM-DD-YYYY")
reservvar.create(query,callback);
}
module.exports.getReservations = function (callback){
reservvar.find({},callback);
}
Index.js file :
app.get('/api/reservations',function(req,res) {
Rezervari.getReservations(function(err,reserv){
if(err){
throw err;
}
var changetime = reserv[1].data;
console.log(reserv[1].data)
changetime = dateformat.utc(changetime).format("MM-DD-YYYY") // this one returns the date in desired format but with wrong values as stored in db
console.log(changetime)
res.json(reserv);
});
});
app.post('/api/createrezervare', function (req,res) {
const reserv = req.body
const name = reserv.name
const numberofg = reserv.number
const phone = reserv.phone
const email = reserv.email
const data = reserv.date
const timetable = reserv.time
const furtreq = reserv.frequests
Rezervari.createReservation({name:name,numberOfGuests:numberofg,phone:phone,email:email,data:data,timetables:timetable,furtRequests:furtreq},function(err,reserv){
if(err){
throw err}
res.json({status:true})
})
})
You are inserting a Javascript Date Object from Node.js, and that same Date is being inserted in MongoDB, it's being inserted correctly.
I think you are confusing how dates are stored internally and how are they formatted when you print them.
When you check the content of your data in MongoDB it's just shown in that particular format, an ISO date. If you take a close look at the date shown you can see a Z a the end, Z means "zero hour offset" also known as "Zulu time" (UTC).
In Javascript when you create a Date object without setting the timezone, it's by default created in your system timezone. Also, Date objects are not stored with any format, nor in JS nor in MongoDB. In JS, dates are stored internally as time values (milliseconds since 1970-01-01).
Supposing we are in Japan, JST time (UTC+9):
const d = new Date("09-20-2018");
console.log(d.getTime()); // 1537369200000
console.log(d.toString()); // Thu Sep 20 2018 00:00:00 GMT+0900 (JST)
console.log(d.toISOString()); // 2018-09-19T15:00:00.000Z
First we are printing out the number of ms, after the Date including the timezone, and finally the ISO Date, almost same format that MongoDB uses to print dates in the Mongo shell (anyway, in UTC).
So, new Date("09-20-2018") is going to store the milliseconds until 09-20-2018 00:00 in Japan Time. Then, if you insert that object in MongoDB, internally it will store the correct date (I don't know internal details of MongoDB, but maybe it's storing the milliseconds as well).
If you check MongoDB you will see something like ISODate("2018-09-19T15:00:00Z").

Categories