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()
}
Related
I have some entries in a database and and they all contain a date string.
I have some TS like this
ref.LimitToFirst(10).orderByChild('date')
The problem i an having is that TS treats this as a regular string and just orders by the first number in the date. ex - 10/1/2021 would be ordered before 2/1/2021. How could i fix that ?
You can't fix this in code while depending on the database to perform the query. In Realtime Database, strings will always sort lexicographically - by their natural string order. You can't make it interpret the strings as something other than a normal string.
You will have to instead store an integer value that you can sort chronologically. In JavaScript, you can simply create a Date object and use its getTime() method field to get the Date's representation in millis since the unix epoch.
If you can't store a proper timestamp, then your only option is to read all the data and sort it yourself in your app, which will not scale well.
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.
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"
}
})
I'm trying to fetch data using http://api-sandbox.oanda.com/v1/history?instrument=EUR_USD&candleFormat=midpoint&granularity=W&start=1351728000000&end=1368144000000
to get data in between to dates but it showing:
{
"code" : 45,
"message" : "Invalid timestamp: [start] parameter",
"moreInfo" : "http:\/\/developer.oanda.com\/"
}
What I did wrong? I'm using this reference to set sart and end date. http://developer.oanda.com/docs/v1/rates/
According to docs you posted, you should be using RFC3339 format, not timestamp:
start:
Optional
The start timestamp for the range of candles requested. Must be specified in RFC3339 format.
Examples:
2002-10-02T15:00:00Z
2012-11-01T00:00:00Z
2013-05-01T00:00:00Z
And proper encoded URI:
http://api-sandbox.oanda.com/v1/history?instrument=EUR_USD&candleFormat=midpoint&granularity=W&start=2012-11-01T00%3A00%3A00&end=2013-11-01T00%3A00%3A00
Mongoose schema includes this:
{
dueDate: { type: Date, required: false }
}
My goal is to fetch docs sorted by dueDate with the soonest date at the top, followed by the rest with no dueDate. My problem is that a due date is not required therefore docs with no dueDate get sorted above docs with a valid date.
This is how I sort it (node.js & mongoose.js):
query.sort({ dueDate: 'asc' });
I'm mainly looking for a built in way to do this; I've already implemented a hack by combining queries.
Here is a workaround for date type which will require 1 additional field per such date field to be sorted (This solution can be used for numbers as well)
Store negative epoch of the non null dates (or -ve of number) in this new field.
Sort by date ascending would mean sorting descending by this -ve epoch. Nulls will be at the end as that is the way MongoDB sorts data in descending order. The actual date (or number) field will appear in ascending order, with nulls at end.
If there is need for nulls to be at and while sorting the date (or number) in descending order, sort on original date (or number) field instead of -ve epoch.
there is no built in way to do that. There is a ticket opened to make helpers available to do customized sort functions, it is planned but not schedulled: https://jira.mongodb.org/browse/SERVER-153
Check this thread: Is there a way to put all the nil value at the end when sorting with mongodb/mongoid?
Regards,
Moacy