How to sort MongoDB collection with day from createdAt field with another field? - mongodb

Is there any way i can sort a collection descending of day from an createdAt field.
What I'm expecting is that, I wanted to sort contents based on created day from createdAt field with rating field. So that i will get all the good rated content for each day
If i do { rating: -1, createdAt: -1 } i'm not getting the desired output

Related

Mongodb query to get document count for each month of a specific year

I have a time field as below in my collection.
"time" : NumberLong(1531958400000)
I first want to query documents by the current year(using $match) and then get the document count for each month.
I have managed to match the year using the below query.
db.myCollection.aggregate([
{$project: {
year: { "$year":{"$add":[new Date(0),"$time"]}}
}
},
{$match: {year: 2021}}
])
How can I write a mongodb query for the mentioned scenario?
Thanks in advance!
You can do following:
parsed the timestamp using $date
compare the parsed date field with current year
$group by $month value to get the count
Here is the Mongo playground for your reference.

Mongoose query by current date

So I have a mongoose collection. This collection have a day property in it.
So let's say I have 3 documents in it:
{day: "2017-07-16T17:00:00.000Z"}
{day: "2017-07-17T17:00:00.000Z"}
{day: "2017-07-18T17:00:00.000Z"}
Each date has hardcoded 17:00 time in it. Consider this as the start time.
So let's say currentDate is 2017.07.17 and it's 16h. I need to return document that has day that is 16th, because of the time.
Next one starts in an hour, and in an hour I should be returning the 17th.
I tried:
Table.find({ day: { $lt: Date.now() } })
But this, on the 18th, still returns 16th.
How do I write this query so it returns document that is currently 'active' ?

Find all records older than one year

I have a collection named "listenedMessage" in my MongoDb and it has a field called "timeReceived"
How do I find all records which are older than one year based on the "timeReceived" field?
Based on #Puneet Singh's comment I've came to a solution which retrieves all the records having "timeReceived" field older than a particular month.
function addMonths(date, months) {
date.setMonth(date.getMonth() + months);
return date;
}
db.listenedMessage.find({"timeReceived" : {$lt: addMonths(new Date(), -1)}}).count(); //this will give all the records having timeReceived field older than a month from today

elasticsearch filter dates based on datetime fields

assuming I have the following nested document structure, where my document contains nested routes with an array of date time values.
{
property_1: ...,
routes: [
{
start_id: 1,
end_id: 2,
execution_times: ['2016-08-28T11:11:47+02:00', ...]
}
]
}
Now I could filter my documents that match certain execution_times with something like this.
query: {
filtered: {
query: {
match_all: { }
},
filter: {
nested: {
path: 'routes',
filter: {
bool: {
must: [
{
terms: {
'routes.execution_times': ['2016-08-28T11:11:47+02:00', ...]
}
},
...
]
}
}
}
}
}
}
But what if I would like to filter my documents based on execution dates. What's the best way achieving this?
Should I use a range filter to map my dates to time ranges?
Or is it better to use a script query and do a conversion of the execution_times to dates there?
Or is the best way to change the document structure to contain both, the execution_date and execution_time?
Update
"The dates are not a range but individual dates like [today, day after tomorrow, 4 days from now, 10 days from now]"
Well, this is still a range as a day means 24 hours. So if you store your field as date time, you can use leverage range query : from 20-Nov-2010 00:00:00 TO 20-Nov-2010 23:59:59 with appropriate time zone for a specific day.
If you store it as a String then you will lose all the flexibility of date maths and you would be able to do only exact String matches. You will then have to do all the date manipulations at the client side to find exact matches and ranges.
I suggest play with range queries using Sense plugin and I am sure it will satisfy almost all your requirements.
-----------------------
You should make sure that you use appropriate date-time mapping for your field and use range filter over that field. You don't need to split into 2 separate fields. Date maths will allow you to query just based on date.
This will make your life much easier if you want to do aggregations over date time field.
Reference:
Date Maths:
https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#date-math
Date Mapping : https://www.elastic.co/guide/en/elasticsearch/reference/current/date.html
Date Range Queries:
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html

Query mongo on timestamp

I want to query Mongo based on timestamp. Follwing is the field in mongo.
"timestamp" : "2016-03-07 11:33:48"
Books is the collection name and below is my query for time period of 1 minute:
db.Books.find({"timestamp":{$gte: ISODate("2016-03-07T11:33:48.000Z"), $lt: ISODate("2016-03-07T11:34:48.000Z")}})
Also is there any alternative like I don't have to give greater and lower limit on timestamp. But query based time interval mentioned. Something like, if present timestamp is TS = "2016-03-07T11:33:48.000Z" then query should be between TS and TS + 1 minute rather than explicitly mentioning timestamp. Something like adding 1 minute to present timestamp
db.Books.find({"timestamp":{$gte: "2016-03-07 11:33:48", $lt: "2016-03-07 11:34:48"}})
ISODate is not required here