loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

Where filter with `neq` doesnot work as expected

Open trieuduongle opened this issue 3 years ago • 10 comments

Steps to reproduce

  1. Create a entity has id property as the following
class Sample extends Entity {
@property({
    type: 'string',
    id: true,
    generated: true,
  })
  id?: string;
  // other properties
}
  1. Make a query to find samples that not equal the id. For example
const id = '5f7f32799f3b8031a56e4ca8';
const result = this.sampleRepositoy.find(
  {
    where: {
      id: {
        neq: id
      }
    }
  }
)

Current Behavior

result still has the entity with id='5f7f32799f3b8031a56e4ca8'

Expected Behavior

result should exclude the id that I used to filter. I am using mongo with package: "loopback-connector-mongodb: 5.3.0" Everything will work if I try to install "mongodb" package and replace neq: id with neq: new ObjectId(id)

trieuduongle avatar Oct 08 '20 15:10 trieuduongle

related to #1875, and #3720

mdbetancourt avatar Oct 08 '20 23:10 mdbetancourt

Hi @mdbetancourt Seem issue https://github.com/strongloop/loopback-next/issues/3720 is still opening, so its mean that I should continue waiting for it?

Because I tried to add mongodb: { dataType: 'ObjectID' } into @property decorator or set strictObjectIDCoercion to true in @model but they didn't work as well.

trieuduongle avatar Oct 09 '20 06:10 trieuduongle

@hacksparrow, could you please take a look, since you're working on the ObjectID task? Thanks.

dhmlau avatar Oct 20 '20 00:10 dhmlau

@trieuduong1602 ObjectId configuration can be tricky, please share a minimal app reproducing the issue, I will configure it for you.

hacksparrow avatar Oct 20 '20 06:10 hacksparrow

@hacksparrow sorry for late reply, I just invited to my project "reproduce-loopback-4-bugs" (I marked this project as private as well, so please accept it)

==== Project preparation: after you cloned my project, please do the following steps:

  1. Open terminal in the project root. Inside terminal, calling following command 1.1 git checkout issues/6518-use-neq-statement-does-not-work 1.2 source build.local.sh 1.3 npm run db:deploy 1.4 npm run migrate -- --rebuild 1.5 npm start
  2. open loopback swagger by: http://localhost:3002/explorer/
  3. use api: "/categories" to get all categories that I seeded before. (there are 2 categories only)
  4. Pick one id in the categories list.
  5. use api: "/categories/{id}/neq" with this id ==== Expection: The categories list returned by api '/categories/{id}/neq' should exclude the id that you chose (i.e, there should be return 1 category in the list only, instead of 2)

trieuduongle avatar Oct 24 '20 14:10 trieuduongle

I have same problem when calling findOne. The problem persists even if installed "mongodb" package and replace neq: id with neq: new ObjectId(id).

f-w avatar Feb 08 '21 06:02 f-w

This issue has been marked stale because it has not seen activity within six months. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository. This issue will be closed within 30 days of being stale.

stale[bot] avatar Aug 07 '21 07:08 stale[bot]

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

stale[bot] avatar Sep 06 '21 07:09 stale[bot]

Has there been any progress on this subject? The issue persists with the latest version of loopback.

AlexDM0 avatar Aug 16 '22 08:08 AlexDM0

If anyone seeks for a workaround, this should do the trick:

where: {
  // @ts-ignore
  "_id" : {
      neq: new ObjectId('5f7f32799f3b8031a56e4ca8')
  }
}

rendesg avatar Sep 20 '22 20:09 rendesg