Firestore collection query on timestamp field using epoch timestamp - google-cloud-firestore

Please advise how to query firestore collection on timestamp field?
Query below returns nothing even if there are multiple documents with filled date field (timestamp type) exist in db:
db.collection("users").doc('john').
collection("comment").
where("date", ">", 0).
get()

For you to be able to query using date fields, you need to compare a date - in this case, the date field from your document - to a date as well. The 0 is not considered a number 0, so this affects the comparison.
It needs to be something like this: where("date", ">", new Date()). In case you want to compare dates based in epoch timestamp, a good solution is convert the new date using the where like this: where("date", ">", new Date(epoch*1000)) as mentioned. Once you using comparison in the where part of the query, the returns should occur correctly.
Besides that, more examples of converting timestamps for Firestore can be found here.

Related

String comparison with gte and lte in mongodb

I have a field in one of my mongodb collection that stores date in below format:
{
name:"ABC",
LAST_UPDATED:"10/10/2022 6:0"
},
{
name:"ABC",
LAST_UPDATED:"10/10/2022 6:15"
}
...
New data is added to this collection every 15 min.Everyday this collection will be cleaned at 12AM, so the date would be same in every document. Now I want to query for documents which have LAST_UPDATED time between 6:30 and 8:45. I have written below query:
db.collection_name.find({$and:[{LAST_UPDATED:{$gte:"10/10/2022 6:30"}},{LAST_UPDATED:{$lte:"10/10/2022 8:15"}}]});
The issue is that above query works only for some time ranges. It doesn't work for every input. I cannot also use mongodb date format.
Is there any way to find documents in a given time range considering that the format of LAST_UPDATED field will not be changed?
Thank You!

Is there a way we can get date alone from a datetime field stored as string in MongoDB. We are using Spring MongoDB driver

Let's say I have an date attribute stored in the format dd/MM/YYYY HH:mm:ss i.e. 22/12/2021 10:15:23. Now I just want 22/12/2021 as a date value so that I can do date comparisons. I want this operation to happen at database itself so that I can do date comparisons as part of the query/aggregation itself. Please note I am using Spring MongoDB driver.
Getting date alone from a datetime string is not possible in MongoDB since it always returns an ISO date with time. As a workaround we can convert the time HH:mm:ss to 00:00:00 either by applying String operations on the string attribute or by using Arithmetic operations on the date. We will have to add a temporary field in the query which will hold the converted value using add field operations. And then we can compare them with a given date.

Is ISO8601 the best date-format for PostgreSQL jsonb when i want to filter by the date?

I'm new to PostgreSQL and I have the following question:
I have a table with just an id-column and a data-column, which uses the jsonb-type. Inside the jsonb-object I have a datetime field. I read in various posts, that I should use the ISO-8601 dateformat to store in the DB.
I want to filter my table by date like this:
SELECT * FROM table WHERE data->'date' > '2016-01-01T00:00'
Is this really the best date-format for this purpose?
Thanks in advance :)
IMHO Your query should produce
ERROR: operator does not exist: jsonb > timestamp with time zone
If I get it right. In case you change -> to ->> it should get a text value instead of jsonb field (which is also not comparable to timestamp).
It should be smth like
SELECT * FROM table WHERE (data->>'date')::timestamptz > '2016-01-01T00:00' to work
The big advantage of that format is that string order corresponds to date order, so a comparison like the one you quote in your question would actually work as intended.
A second advantage is that a timestamp in that format can easily be converted to a PostgreSQL timestamp with time zone value, because the type input function understands this format.
I hope you are not dealing with dates “before Christ”, because it wouldn't work so easily with those.

Retrieve an ISO type date, convert into DateTime and find oldest date from a set of docs

{
id:ObjectID(12fgrh567d8vb9567890578192)
.
.
.
meta:{
'date':ISO(1997-07-16T19:20:30.45+01:00)
}
}
I have this kind of about 500 documents, what I intend to do is define a function in using (pymongo) to retrieve the oldest date from all the documents. I am confused whether I should use the $min function in the ISO format or should I convert it to date time and then go for the $min.
Use following query:
db.collectionname.find({}).sort({'meta.date': 1}).limit(1);
this will first sort all the documents with the meta date. and will return the oldest one.
$min will directly work on IsoDate. No need for conversion.

to store date in mongodb

I need to store date in mysql (without time). User inputs date in input box like yyyy-mm-dd, may be later fomat could change.
Could you please tell what is good way to store date in mongodb (without time), we'd use DATE type in mysql? Now whe I need to store date and time I use mongdb date type.
And store it like this:
$data['ADDED'] = new MongoDate(time());
And display them:
echo gmdate('Y-m-d H:i:s', $data['ADDED']->sec);
When I use only date I store them as string like: yyyy-mm-dd (I validate date before storing it to make sure it's correct date). I'll need to find by date something like this:
dateField <(>) somedate
Do you think it's acceptable to store date as string in mongodb? How do you usually store date in mongodb?
MongoDB does not have a DATE type. It instead has a ISODate type. This is just as good for storing "without" time as I will explain.
So you can use MongoDate like so:
$date = new MongoDate(); // Denotes today, just like the date() function
Now to store without time you can just fake it by making PHP only set a date, as such the default time will be 00:00:00 (I should note this does mean a time is actually stored just as 00:00:00):
$date = new MongoDate(strtotime('2012-04-23')); // strtotime for fun
And then you can query by just the date part like:
find(array('date' => new MongoDate(strtotime('2012-04-23'))));
And you can now query as though you don't have a time since the time will equal what you put in: 00:00:00.
Of course this is just one way of fixing it.