I want to compare 2 iso dates using only the day without time, in Mongo DB. I want this query just like SQL.
select * from data where DATE_FORMAT("data.effectivestartdate", "%Y-%m-%d")
< DATE_FORMAT("2018-11-13T23:59:59+05:30’", "%Y-%m-%d");
How can I write this query in Mongo DB?
Kindly modified this query.
db.entityrecord.find({
"data.effectivestartdate": { "$lte": ISODate("2018-11-13T23:59:59+05:30") })
Related
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!
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
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
I have a conn_time field in the collection,need to export last 15 mins data from the collection using mongo export .conn_time date is in ISO format.
date format like 2013-06-11T07:18:00Z. I have tried using JavaScript like but not working
var enDdate = new Date(); enDdate.toISOString()
var startDate = new Date(endDate-15*60*1000); startDate.toISOString();
You can check out this question: mongoDb return results based on time interval
You have to escape the spec chars. I could not make it run with the ISODate helper but like this
"{date:{\$gt:{ \"\$date\" : 1370935140000 }}}"
given as a -q parameter to the mongoexport works fine. This case 1370935140000 is the unix timestamp in mili secs. So you have to calculate the unixtimestamp 15 mins before than add 000 at the and and go on with this. I will try to resolve also with the ISODate.