So I have this field called task_time stored as string in Mongodb in 'YYYY-MM-DD' format (eg. '2012-12-21').
Now I need to query this collection to get the data whose task_time is within a given time interval.
The time interval is given as a pair of strings that represent start time and end time in 'YYYY-MM-DD hh:mm:ss' format (eg. '2015-12-21 16:00:00').
Is there any way to do this directly in Mongo query without bringing this task to my javascript code?
As I know $gte and $lt can work with strings too. The same in mongoose after this issue
items.find({
task_time: {
$gte: "2015-12-21 12:00:00",
$lt: "2015-12-21 16:00:00"
}
})
Related
Good morning and thanks in advance. I tell you my problem, I am making a filter to search for requests for certain specific elements and when searching for a date in YYY-MMM-DD I had the problem.
It turns out that mongo saved it to me with this format:
createdAt
2022-07-10T18:11:28.630+00:00,
updatedAt
2022-07-10T18:11:28.630+00:00
which I was able to "fix" with:
if (payload.date) {
query.createdAt = {
$gte: new Date(`${payload.date}T00:00:00.000-00:00`),
$lt: new Date(`${payload.date}T23:59:59.999-00:00`)
};
but my problem arose that when saving and searching by date, it seems that I save it with timezone zero, so I consult the date 2022-07-11 and it did not bring any data, I tried with 2022-07-12 and it just returned a response of documents.
I think my solution would be to use moment and convert it to utc +5 when doing the query. Does anyone have an idea how I can do that fix? I would appreciate it very much.
Maybe similar to this
query.createdAt = {
$gte: moment.tz(payload.date, "America/Toronto").startOf('day').toDate(),
$lte: moment.tz(payload.date, "America/Toronto").endOf('day').toDate()
};
I'm trying to scan a table and return all entries which match my filter expression. I've got an API Gateway which accepts a GET-Request that contains a starting time and a end time. (Format: [dd.mm.yyyy hh:mm:ss]
The database im scanning has an attribute called "timestamp" in the same format. I've tried it with the filter
Timestamp BETWEEN :date1 and :date2
date1 and date2 represent the starting and ending time from the API. Timestamp is the field in my Database.
Unfortunately the scan doesnt return any values.
Thanks a lot.
Your date format has to be in ISO 8601 string format for you to be able to scan on it. You can see this in the documentation here, under the "String" category.
I need to insert to database two variables:
- "start" with value "9:00"
- "end" with value "20:00"
It can be types String or Timestamp, i would prefer the string. But in database table the fields are "Time" type. So how to cast it? I got only time values without full date like month, days and years.
From what I have read, if you want to insert a string into a MySQL TIME column, it will have to have the format HH:mm:ss. So the easiest solution for you might be to obtain your start and end times in the format of 09:00:00 and 20:00:00.
I expect the following INSERT statement to proceed without error:
INSERT INTO yourTable (`time_from`, `time_to`)
VALUES
('09:00:00', '20:00:00')
To be clear, I would recommend putting in a little effort in your code to obtain time data in this format. Then just insert it into MySQL without further hassle.
In a Date Field you van only Store Dates without time Information.
So you Must change the fieldtype or add a nee Field for this
I know that it's possible to query by timestamp as seen in previous question, However, what if the date is in the following format:
{
"lambeosaurus": {
"date" : "2012-20-03",
"length" : 12.5,
"weight": 5000
},
"stegosaurus": {
"date" : "2015-25-13",
"length" : 9,
"weight" : 2500
}
}
How would I go about and query this by date?
You've stored your dates as strings. When Firebase order strings it uses lexicographic ordering. In that order, "2012-20-03" comes before "2012-25-01".
This is the reason why the question you linked to (and the Firebase documentation) usually store dates as timestamps. Those have all the information about the date, in a single number that is guaranteed to be ordered correctly.
Alternatively, you can store the date as a string. But in that case you have to make sure the date is in a format that will lexicographically order correctly too. For your sample that would be: "2012-01-25" and "2012-03-20".
So in this case your only option is to change the data structure to either what was in the original question (and documentation) or to a string format that orders in the order you want.
Convert it to timestamp :
new Date('2012-20-03'.split('-').reverse().join('/')).getTime()
In general :
function toTimeStamp(dateString){
return new Date(dateString.split('-').reverse().join('/')).getTime()
}
I want to find all the users created up until yesterday. This is my code to make the query string:
var today = new Date();
var a = today.getDate();
a--;
today.setDate(a);
var yesterday = today.toDateString();
and it returns something like: Sun Jan 17 2016... which IS yesterday's date, but the db stores the date in iso format like: "date" : ISODate("2016-01-13T13:23:08.419Z") so what I get is 2016-01-13T13:23:08.419Z. My problem now is that I can't use the yesterday variable to query the db for the users I need AND even if I could, I don't know how to find every registration not including the ones that took place today.
Any help? Thank you very much!
You are generating a date on the front end, and then pushing it back a day, which is totally fine for a lot of circumstances -
For this, since you are trying to find DB entries that occured, perhaps try querying the database with a timestamp ranged pulled from the ID's of each document in your database.
Here is some documentation on how to do that from mongoDB. https://docs.mongodb.org/v3.0/reference/method/ObjectId.getTimestamp/
I've also provided some additional resources that may help you figure out exactly what to query in regard to this method:
https://steveridout.github.io/mongo-object-time/
https://gist.github.com/tebemis/0e55aa0089e928f362d9
Some psuedo code:
1. Query documents in the database
2. Get a timestamp from the ID's of the documents in the database
3. Set a range of the timestamps
4. Compare returned timestamps vs a timestamp range variable (yesterdays date in this case)
5. Have the DB return only documents that are within the range
I hope this helps!
Try this, using Moment.js:
yesterday = moment().add(-1, 'days');
db.users.find({ "date": { "$lt": yesterday }});
Create a date object that represents the start of day today, use it to query your collection for documents where the date field is less than that variable, as in the following example
var start = new Date();
start.setHours(0,0,0,0);
db.users.find({ "date": { "$lt": start }});
This will look for users created up until end of day yesterday.
momentjs is a super handy utility for doing manipulations like this. Using the library, this can be achieved with the startOf() method on the moment's current date object, passing the string 'day' as arguments:
Local GMT:
var start = moment().startOf('day'); // set to 12:00 am today
db.users.find({ "date": { "$lt": start }});
For UTC:
var start = moment.utc().startOf('day');