Im trying to create a query to search for all collections that are in between of two Date strings, however all my attempts have failed and every time I try a query it always return no results
this is the query im using:
const intialDate = new Date('2024-05-23T06:00:00.000Z');
const endDate = new Date('2024-05-24T20:44:02.172Z');
const dispoquery = this.guestModel.find({
$and: [
{
$or:[
{
initial: {
$gte: intialDate
}
},
{
end: {
$lte: endDate
}
},
]
}
]
}).catch((err) => {
return err;
})
.then((doc:any)=> {
return doc
})
I need all documents that comply this assortment:
intial >= intialDate || end <= endDate
But I always get no results this is a sample data of my collection:
{
"folio": "W1013",
"nombre": "CArlos",
"initial": "2024-05-23T06:00:00.000Z",
"end": "2024-05-24T19:52:11.050Z",
"__v": 0
}
Can someone please tell me what im doing wrong please?
Some issues with your data and query:
Your query has the words initial
& end
but your DB document has llegada
& salida
.
Your documents have the dates as Strings, not as Date Objects. And you are comparing with Date objects, not Strings.
llegada
& salida
to dates using $dateFromString
.$dateFromString
during the queryThere's no need to use $and
if it's just one $or
clause within it.
As cmgchess commented, if you are looking for something between two dates, you should only use $and
, not $or
.
But your question says
intial >= intialDate || end <= endDate
So I'll keep the logic the same as per what you already have. But you should probably do
intial >= intialDate && end <= endDate
Since some conversions need to be done on the document data while querying, you'll need to use $expr
with your comparisons/checks.
db.guestModel.find({
$expr: {
$or: [
{
$gte: [
{ $dateFromString: { dateString: "$llegada" } },
intialDate
]
},
{
$lte: [
{ $dateFromString: { dateString: "$salida" } },
endDate
]
}
]
}
})