I want to ges all the documentos that are inside a range of dates, but my query instó retriving all the validar Collections:
To resume
My query receives two date values as a string, one for ‘arrival’ and the other for ‘check-out’ with these two dates i need to retrive all documents that are between these two dates.
Ex.
arrival=’2024-06-11T06:00:00.000Z’
check-out=’2024-06-16T06:00:00.000Z’
with these values i should retrive all documents in which the ‘arrival’ value is ‘greater than or equal’ to the arrival input and ‘less than’ the ‘check-out’ value, but it should also get all the documents in which the ‘check-out’ value is ‘greater than’ the the arrival input
And i dont know how to the $or clase in the query for the ‘check-out’ case
My Query:
db.collection.find({
$expr: {
$and: [
{
$gte: [
{
$dateFromString: {
dateString: "$arrival"
}
},
new Date("2024-06-11T06:00:00.000Z")
]
},
{
$lt: [
{
$dateFromString: {
dateString: "$arrival"
}
},
new Date("2024-06-16T06:00:00.000Z")
]
}
]
}
})
Mongo playground Link : https://mongoplayground.net/p/1HRTg2eLeDa
2
Answers
Finally manage to complete the query, thanks to the anwsers of everyone i manager to come up with the solution, this is how the query ended op like:
If you store values as
Date
objects and if I understand you requirements correctly, it would be this one:The
$and
operator is not needed.