I'm having a problem while querying for a particular date in my database
I need to get the data on a particular date
I'm using $gte and $lt mongo functions to conditionally query the date
But I'm not able to get data which corresponds to the date which i need
I'm receiving back all the data from that date till today's date
Because of the $gte(greater than or equal to) function
I'm not able to find a proper solution for this
I tried to look in stackovflw but still couldn't find it
Can someone help me!
This is the query using mongoose I'm trying to get the data only for that particular day
Attendance.find({teacherID:req.body.id,subject:req.body.subject,grade:req.body.grade , section:req.body.section,date:{"$gte": new Date(req.body.date)}}
Maybe you can read the documentation about filter by date at mongoose doc
Related
I am trying to get only date pate part from timestamp value using DATE() in MySQL its showing result correct in MySQL but in node js its showing full-time stamp instead of date.
Example : -
SELECT DATE("2020-04-12T18:30:00.000Z");
Generating 2020-04-12 in MySQL but in node js while fetching through my own API it's giving "2020-04-12T18:30:00.000Z" this.
Help me to Solve this.
You can use in the query SQL this:
SELECT ...,(DATE_FORMAT("2017-06-15", "%Y-%m-%d") as finalDate) FROM ...
And now in your API, when you call to MySQL with the previous query you should have 2017-06-15 in finalDate. The type DATE in MySQL is a Date Object, so if you want only date in string, you need to cast it in MySQL with the previous code or in your API with parsing date to string with methods or Date
I try to dynamically create a mongo query with dates.
This is a part of a JSON query I produced:
{"$or":[{"createdAt":{"$gte":"2017-08-31T22:00:00.000Z"}},{"modifiedAt":{"$gte":"2017-08-31T22:00:00.000Z"}}]}
but it does not work.
This is a part of code which stands behind it:
let result = {$or: [{createdAt: {$lte: new Date(date)}}, {modifiedAt: {$lte: new Date(date)}}]};
Spent 3 hours trying to find a solution. My understanding is that $lte here tries to compare mongo date object with a string describing date in ISO format. And it does not work well.
I do not know how to create a proper JSON object being a mongo query containing dates.
Please help! :-)
If you want to query Mongo with date range, you have to use a standard Date format (ISO-8601). To achieve this, try with moment:
let date ="2019-02-22"; //for example, in my apis, you can set multiple type of date (YYYY-MM-DD, YYYYMMDD, DD-MM-YYYY,..) but date format with / is forbidden.
let query = {$or:[{createdAt: {$lte: moment(date).format()}}, {modifiedAt:{$gte: moment(date).format()}}] //pass parameter to format, in this case it will use the default locale format
Like this you are setting a query. IMO querying on date range with "OR" condition is not useful: usually a date query in is "AND" condition (if you are querying on a single date, your query will be: greater than today or lower than today -> everything)
Using GET I receive an array and one of the data items is a timestamp string in the format:
"date":"2017-01-11 12:19:15",
I have a function that needs to show specific data from this GET request for the current day only.
I think I would need to run an each loop and select only items that have the date: "2017-05-28", ignoring the "12:19:15" part.
Is there an easy and clean way to do this?
Many thanks!
Use Date.parse() (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date/parse) to parse the date, then iterate over the list of parsed dates, only selecting those within the range you seek.
When using Date.parse(), you might want to be familiar with this Why does Date.parse give incorrect results?
I am sending two fields from client side date and time.
date will be in the format of YYYY-MM-DD i.e 2016-11-08 and time will be in the format of 05:30 PM or 09:45 AM.
I want to combine these two fields and create new field say added_datetime , this field going to be inserted inside MongoDB, so I want it to be in the form of Mongo Date Object so that I can use it for searching with date.
Tried some random things using moments.js but unable to get what I want.
As mentioned in a similiar question you can create a date object with
var date = new Date(datestring);
The code
var startDate = new Date("1900-1-1 8:20:00 PM");
which the original questioner supplied works in chrome and should also work in node since it's the same js engine. This seems to answer your question.
You can find more on dates in the MDN documentation and MongoDB documentation.
I want to display a list of entries only if they are dated in the past. My goal is to be able to queue articles that will posted at a future time.
My entries all have a javascript date object timestamp when I insert them to the database. How can I make sure that entries dated in the future don't show up in my results?
You could use a query like this to publish the data to the client.
MyCollection.find({
date : {
$lte : new Date()
}
});
$lte makes sure that the date field is
'less than or equal to' new Date() (the current time)