Skip to content

Using two predicates inside one object ignores one of them #4868

@joeytwiddle

Description

@joeytwiddle

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

  1. If that badDateFilter is 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.

  2. (preferred) Fix whatever is causing the lt to be ignored, so the badDateFilter will actually work as intended.

  3. 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 and clause 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugdeveloper-experienceIssues affecting ease of use and overall experience of LB usersstale

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions