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
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!
Are you able to use MongoDB to combine rows of data into one row?
I'm using dates with year, month, day and hour. The data is shown per hour. Is there a way to combine data of the hours into just one day with data. I would basically remove the hour column and sum the hour data into per day data.
I'm not sure what you mean by "the data is shown per hour" - do you mean it's stored in the database that way?
MongoDB doesn't have rows and columns - the equivalent of a row is a document, and the column equivalent is a field. Unlike in traditional SQL, a field isn't just one piece of information (a string, number/date, boolean, null, etc). It can be more than one piece of data - it can be an array, or a document, or an array of documents, etc.
Anyway, based on the small amount of information I have on your situation, I'd absolutely design the data with the bucket pattern. https://www.mongodb.com/blog/post/building-with-patterns-the-bucket-pattern
You could $unset the 'measurements' array and just keep the sum/count fields if that's what you want.
If your data is already set in stone, then I'd use an aggregation pipeline to group all the documents ('rows') together - the group _id would be year, month, day, and you could sum/count/min/max/etc the data in the group too.
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 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") })
I've got this collection in the db
[{name:1,startDate:1/1/13,endDate:2/2/13,number:10},
{name:1,startDate:2/2/13,endDate:5/2/13,number:15},
{name:2,startDate:2/1/13,endDate:5/2/13,number:25},
{name:1,startDate:5/2/13,number:17},
{name:2,startDate:5/2/13,number:30}]
I want to get a list of start dates with the average of the number within the range of this start date, for example, the result of this collection would be:
{date:1/1/13,avg:10},
{date:2/1/13,avg:17.5},
{date:2/2/13,avg:20},
{date:5/2/13,avg:23.5}
I've tried some map-reduce and group but none can group by start date properly,
please help.
You can do this by using the aggregation pipleline:
db.collectionName.aggregate([{$group:{_id:'$startDate',avg:{$avg:'$number'}}}])
This will group the collection by startDate and with the aggregation being the average of the number.
If you strictly want the _id field labelled as startDate, you use the $project operation in the pipeline:
db.collectionName.aggregate([{$group:{_id:'$startDate', avg:{$avg:'$number'}}},{$project:{'startDate':'$_id','avg':'$avg',_id:0}}])
This will give the desired result.
For lack of a better way i just did it from the server side,
first getting all the startDate s and then preforming an iteration that finds every document in the date range and averaging it's number...
Not quit how i expected to solve this, but it works properly.