Hello, please, I need to make a query to rescue the information from yesterday. Example "{type: “REFUND”, Date: “yestersay”} in mongodb compass
Unless I’m being stupid I have no idea what you need from your post.
Are you trying to recover deleted data or use Compass to find data or see what queries you ran yesterday?
If you want help with queries, please attach sample documents so people can see exactly what’s going on without making (too many) assumptions.
I need to make a query to retrieve data from yesterday from a database, but I don’t want to add the date
Ahh, ok so you have a database with a collection, lets call them Database and Collection and the collection has documents like this:
{
_id:'XXXXXXX123456',
documentDate:ISODate('2023-08-10 11:23:000000')
}
And you want a query that does always gets data from yesterday without needing to update it every day to enter yesterdays date?
Or…
Do you want to extract the document date from the ID field and then use that to look for documents that were created yesterday as opposed to a field you’ve set on the document being for yesterday?
Exactly, I need to execute this query every day to extract data from the previous day
And what do your documents look like? What field determines documents from the previous day? The ID ObjectID or a field that’s set on the document?
This is the query {type: “REFUND”, accountingDate: “2023-08-09”} and it works but I have to run it daily and I want the date to be added automatically from the previous day
That’s not an actual date then, it’s a string? And what format is the date, I’m assuming it’s ISO as opposed to something exotic like YYYY-DD-MM.
Ok, so if you want to compare to a calculated date you want to use $expr operation:
This means you can use all the aggregation options and compare data (similar to if you want to compare two fields within a document against each other in a find).
With the aggregation style query comes the ability to use aggregation variables such as $$NOW:
You’ll want to compare the field in the doc against the current date, with one day taken away then re-formatted to YYYY-MM-DD format:
db.getCollection("Test").explain().find(
{
$expr:{
$eq:[
'$accountingDate',
{
$dateToString:{
date:{
$dateAdd:{
startDate:'$$NOW',
unit:'day',
amount:-1
}
},
format:'%Y-%m-%d'
}
}
]
}
}
)
Something like this, if doing this on a lot of data, obviously create an index…
thank you very much problems solved thank you