Mongo Atlas filter by date is not working - mongodb

I am using the the following query to filter dates greater than the 25th of May, but it does not work. In the UI I chose the $match operator
{
query: {$date:{ $gte:ISODate('2022-05-25')}}
}
In fact in the resulting collection it returns also dates prior to the one that I have specified in the filter condition.
Can you point me to the exact query?

Related

mongoDB data filter by range

I have one collection where I want to filter data based on a range.
Range will go like this:
Last week (last_week)
Last month (last_month)
Last quarter (last_quarter)
I want to take input as last_week or last_month or last_quarter and based upon the input given, I want to filter data from documents matching the criteria supplied.
Is it possible to build a criteria on the fly based upon the input given?
Yes, you can. Build your criteria like this,
query = {};
query.createdDate = {
$gt: last_week,
$lt: last_month,
};

How to embed the date-to-timestamp conversion in a mongodb query?

Suppose I have documents in a mongo collections with a timestamp field. I want to query the documents based on the timestamp value (greater than, less than). In order to do that, instead of calculating each time the timestamp for my current date, I would like to embed the conversion from the date to timestamp in the query. I tried something like this (suppose "ts" is my timestamp field):
db.my_collection.find({"ts": {$lt: {$toLong : new Date("2020-12-09")}}})
instead of doing:
db.my_collection.find({"ts": {$lt: 1578524400}})
but it seems to return an empty result.
Is there a result to perform the date-to-timestamp conversion inside the query?
You can use $toLong but it's an aggregation pipeline operator, you need an aggregation pipeline not a plain find.

Mongo find all documents from a particular month

I have a collection with documents containing a date.
Is there a way to find all documents from a particular month (all years), using db.myCollection.find()?
I managed to achieve approximately what I need by using the aggregation pipeline and the $month operator, but I couldn't find anything similar among the 'query and projection' operators.
I do not need to group the documents (I only need to filter them).
I am modifying some code that creates filters dynamically based on the user selection and the code uses find with the generated filters.
I would like to be able to give a user the ability to see only entries from particular months (say, only entries for the summer season).
Finally is there a performance difference between using a filter with find and using the equivalent filter in a $match stage with the aggregation pipeline ?

View last N documents using MongoDB Compass

I wish to view in MongoDB Compass the last N documents in a very large collection; too many to scroll through.
I could .skip(total - N) if I knew the syntax for that within Compass.
Alternatively, I have a date field and could use $gte with a date if I knew how to express a date in a manner acceptable to Compass.
Suggestion/example how to do this, please?
MongoDB Compass 1.6.1(Stable)
For date comparison you need to use $date operator with a string that represents a date in ISO-8601 date format.
{"date": {"$gte": {"$date": "2017-03-13T09:51:26.317Z"}}}
In my case the values of date field in Compass and mongo shell are different. So firstly I query the documents in the shell and then copy the "2017-03-13T09:51:26.317Z" from the result to the Compass filter line. In mongo shell it look like:
{
...
"date" : ISODate("2017-03-13T09:51:26.317Z"),
...
}
MongoDB Compass 1.7.0-beta.0 (Beta)
This version have an advanced query bar that lets you input not just the filter (as before), but also project, sort, skip and limit
(#Oleksandr I learned from your effective answer; thank you.)
I've also been shown that the Compass Schema tab allows one to drag a date range on the _id field to apply a filter query for that range. That range can be successively narrowed as desired.
Skip is descibed here
https://docs.mongodb.com/compass/current/query/skip/
In the Query Bar, click Options.
Enter an integer representing the number of documents to skip into the Skip field
Click Find to run the query and view the updated results.

Filtering & sorting by dates in MongoDB's oplog

I'm trying to filter oplog.rs to view operations that were logged after a certain date. But comparison operators don't seem to work with dates in MongoDB:
db['oplog.rs'].find({ts: {$gte: ISODate("2014-12-15T00:00:00Z")}})
What's confusing is a lot of online sources are saying to do it this way but it is obviously not working. I want to return results where the ts field is at least December 15th or more recent, but I am getting results that are clearly before this period. If I switch $gte with $lte no results at all are displayed, even though there are definitely entries both before and after this specified date.
Also, I can't seem to sort the results either. If I try this:
db['oplog.rs'].find().sort({ts: -1})
I get this:
error: {
"$err" : "Runner error: Overflow sort stage buffered data usage of 33566005 bytes exceeds internal limit of 33554432 bytes",
"code" : 17144
}
If I could filter the results to make them more recent, I am hoping it would overcome this sorting error, but I can't even do that with MongoDB's basic operators. How can I filter the results of a find operation by date?
Comparion operators work just fine with date, but ts is a Timestamp object - not a date.
Your query needs to look something like this:
db['oplog.rs'].find({ts: {$gte: Timestamp(ISODate("2014-12-15T00:00:00Z").getTime(),0)}})
This creates a Timestamp object based on an ISODate and uses it in the query.