-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
We are trying to count all records with createTime between two dates. We are using a MongoDB datasource.
const newUserCount = await this.userRepository.count(dateFilter);We tried the following filter, but it does not do what it looks like it should do. Only the gte is being respected. The lt is being ignored, so we end up counting more results that we expected to.
const badDateFilter = {
createTime: { gte: fromTime, lt: toTime }
};Fortunately, if we use an and clause then we get the results we wanted.
const goodDateFilter = {
and: [
{ createTime: { gte: fromTime } },
{ createTime: { lt: toTime } }
],
};So there is a solution.
The real problem is that the lt was being silently ignored, so this went undetected in our codebase for some time.
Possible solutions
-
If that
badDateFilteris illegal, then we could change the TypeScript types to reject it. But I'm guessing that it's a legitimate filter, it's just not being handled well at runtime. -
(preferred) Fix whatever is causing the
ltto be ignored, so thebadDateFilterwill actually work as intended. -
If it's not possible to fix it, then instead throw a runtime error from the library ("This filter cannot be applied correctly"), so that developers will know they need to use an
andclause instead of combining multiple conditions in one object.
Footnotes
In the examples, createTime, fromTime and toTime are all Date objects.
This badDateFilter is quite familiar for MongoDB / mongoose users, although they use $gte and $lt.
We are using the following packages:
├── @loopback/boot@1.6.0
├── @loopback/build@2.1.0
├── @loopback/cli@1.27.0
├── @loopback/context@1.25.0
├── @loopback/core@1.12.0
├── @loopback/repository@1.16.0