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!
Related
While was querying for documents between two dates (the field type is of Date) like -
db.getCollection('ledgers').find({
created: {
$gt: ISODate("2019-01-19"),
$lt: ISODate("2019-02-19")
}
}).sort({'created':-1})
It was returning documents for the year 2018 also.
MongoDB stores times, not dates. You need to convert your dates to times in your application and query by time ranges.
The date on your post [https://i.stack.imgur.com/XIWhf.png] is 2019-02-18 which is in the range you specified in your query.
I suggest you either provide an example of the error at https://mongoplayground.net/
or correct/delete the question itslef.
Thanks
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.
I want to delete all documents created between the dates 2017-08-18 and 2017-08-22. I was told that all documents have CosmosDB's own timestamp under the field '_ts'. I tried using the document explorer to filter the documents using this query c._ts > "2017-08-18" but it does not return any document. I also tried c._ts > 2017-08-18 without the quotes but that returns all documents, even those before that date, just as if there was no query at all. Looking at the documents, I do not even see a '_ts' field. Is there any way I can delete those documents?
Thanks.
_ts is Azure Cosmos DB's internal Timestamp property
The _ts field is a unix-style epoch timestamp representing the date and time. The _ts field is updated every time a document is modified.
If you want to query in a date range, we can do that easliy with udf function, more details please refer to another SO thread.
SELECT * FROM c where udf.udfname(c._ts)>'2017-08-18'
udf function
function epochToDate (ts) {
return new Date(ts*1000);
}
According to
https://msdn.microsoft.com/en-us/library/azure/microsoft.azure.documents.resource.timestamp.aspx#P:Microsoft.Azure.Documents.Resource.Timestamp
_ts gets the last modified timestamp associated with the resource from the Azure DocumentDB database service.
[JsonPropertyAttribute(PropertyName = "_ts")]
[JsonConverterAttribute(typeof(UnixDateTimeConverter))]
public virtual DateTime Timestamp { get; internal set; }
Its is represented as a POSIX or epoch time value. Its the number of seconds that have elapsed since 00:00:00 (UTC), 1 January 1970.
I have a database query that selects all documents having a timestamp field (tmp) falling in a certain range, like so
{ tmp: { '$gte': 1411929000000, '$lte': 1419010200000 } }
This query returns a large number of records, say 10000.
Objective:
To fetch documents in the same interval range, but separated by say (1 hour timestamp) interval in between hence reduce the number of records that is fetched.
Is there a way of doing this entirely using MongoDB query system?
Any comments would be very helpful. Thanks.
EDITED:
Please see the comment below for more info:
MongoDB Filter with Interval
Is there any way to get objectids that are genrated in last 24 hours in mongo collection based on time stamp on objectid and what is the algorithm to genrate objectid in mongodb .
Is there any way to get objectids that are genrated in last 24 hours in mongo collection based on time stamp on objectid
You can call JavaScript code such as:
date = new Date()
date.setDate(date.getDate() - 1)
yesterday = Math.floor(date.getTime()/1000).toString(16)
db.coll.find({_id : {$gt : new ObjectId(yesterday + "0000000000000000")}} , {_id:1})
The first and second statements are straightforward: yesterday's date.
Third row creates a 4-Byte Hex String of yesterday. Which are the 4 leftmost bytes of ObjectId.
Then, you pad the 8 rightmost bytes of the ObjectId with zero's as you don't care about those. These are mac address(3), pid(2) and a running counter(3)
All you have to now is query your collection (coll in the example) and return the _id's
what is the algorithm to genrate objectid in mongodb
There you are