My MongoDB looks something like this :
{"_id": "1234aa", "maturity_dt":"2022-07-10", "countryCode":"BLG"},
{"_id": "123ab", "maturity_dt":"2022-07-30", "countryCode":"BLG"}
Note that maturity_dt is a string. I want to fetch the records with maturity_dt lesser than Today’s date. Please help me build a query with aggregation. Since I have million records I cannot use foreach or any looping.
2
Answers
Depending on your mongo engine you are using in python you will need to format your query something like:
I would also make
maturity_dt
an index in your database.You can perform the comparison with
$toDate
and$expr
. Use$$NOW
to reference today’s date.Here is the Mongo Playground for your reference.
If an aggregation pipeline must be used, you can do like below:
Here is the Mongo Playground for your reference.