Querying for farthest timestamp - mongodb

How can I search my mongodb database for the document with the farthest timestamp? Timestamp being a date, ISODate format or not.

If you use ISODate format you can use $max in aggregate query to get oldest date.
Refer http://docs.mongodb.org/manual/reference/operator/aggregation/max/

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!

Firestore collection query on timestamp field using epoch timestamp

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.

How Spring Data Mongodb convert date from $date to ISODate

Spring Data Mongodb auto parser date object to '$date': millisecond, this query consume more than 1 min to return data, I find ISODate query is more quickly than $date, How I make Spring Data Mongodb parser date object to ISODate.
It is base on your DataType, As i use Instant, mongodb auto convert it to ISODate.
But as you said the date type as millisecond, that cost many time to return data, you should check your query first....
Please show the Object and query for more support

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.

Date comparison in mongodb

I want to retrieve all the documents after a particular date.
My database has date as - DateAdded:"2014-12-17 10:03:46.000Z"
I wrote the following query-
db.collection.find({DateAdded:{"$lte":new Date("2015-06-17 10:03:46.000Z")}})
But the results doesn't fetches any record even though there are records for the dates upto 2015-06-24.
You can use ISODate to compare dates:
"$lte" : ISODate("2015-06-17T10:03:46Z")
ISODate works because that is the format your date is in.
new Date()
wraps the date in an ISODate helper, but is not able to convert in your query.
Check out this link for more information: https://www.mongodb.com/docs/v4.2/core/shell-types/
Use ISODate
format:
{'field': {$operator: ISODate(yyyy-MM-ddThh:mm:ss.msZ)}}
example:
db.getCollection('yourCollection').find({'sampleField': {$lte: ISODate('2015-06-17T10:03:46.000Z')}})
You can use this:
db.collection.find({DateAdded:{"$lte":new Date("2017-11-01")}})
It will convert your date format into this:
ISODate("2017-11-01T00:00:00Z")