loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Where-Filter is not working as expected

Open itinance opened this issue 9 years ago • 4 comments

I've found out several cases where the where-filter is not working as expected. In some cases it works as expected, but sometimes it finds either nothing or retrieves records that does not match the condition. Reproducable.

This is my model:

{
  "name": "message",
  "base": "PersistedModel",
  "strict": false,
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "mixins": {
    "TimeStamp": {
      "createdAt": "created_at",
      "updatedAt": "updated_at",
      "required": false,
      "validateUpsert": true,
      "silenceWarnings": false
    }
  },
  "properties": {
    "id": {
      "type": "string",
      "id": true,
      "index": true
    },
    "sender_id": {
      "type": "string",
      "index": true
    },
    "status": {
      "type": "string",
      "index": true
    },
    "timestamped": {
      "type": "string",
      "index": true
    },
    "message": {
      "type": "string",
      "index": false
    },
    "mime": {
      "type": "string",
      "index": true,
      "default": "text/plain"
    },
    "attachment_processed": {
      "type": "number",
      "index": true,
      "default": 0
    }
  },
  "validations": [],
  "relations": {
    "customer": {
      "type": "belongsTo",
      "model": "team",
      "foreignKey": ""
    }
  },
  "acls": [],
  "methods": {}
}

I can successfully filter for the property "mime":

Either per filter in JSON-Format, passing it to the filter-input in Explorer: {"mime": "video/mp4"}

or per URL:

/api/messages/?filter[where][mime]=video%2Fmp4

But if i want to filter for the field "attachment_processed", it returns none or all:

This will return no results (although there are lots of records in the database) https://...../api/messages/?filter[where][attachment_processed]=0

And this will return ALL records independent of there value in "attachment_processed":

JSON: {"attachment_processed": 0}
URL: `./api/messages?filter=%7B%22attachment_processed%22%3A%200%7D`

The same happens if i pass a value to the equal-condition for attachment_processed:

JSON: {"attachment_processed": 1}
URL: `./api/messages?filter=%7B%22attachment_processed%22%3A%201%7D`

I can pass every value and it always will return all records even if the condition will not met.

Like this record, that was returned by "attachment_processed:1"

{
id: "59784659efd582001188f10da",
sender_id: "5976095de4be7c00103a0548",
hidden: false,
mime: "video/mp4",
attachment_processed: 0,

I can pass every value and it always will return all records even if the condition will not met.

Versions

    "loopback": "^3.3.0",
    "loopback-boot": "^2.23.0",
    "loopback-component-explorer": "^4.1.1",
    "loopback-connector-mongodb": "^3.0.1",
    "loopback-connector-mysql": "^3.0.0",
    "loopback-datasource-juggler": "^3.2.0",
    "loopback-ds-timestamp-mixin": "^3.4.0",

itinance avatar Jul 26 '17 12:07 itinance

After emptying the database the filters work as expected with new data. Seems to be something with upsert. What did i wrong?

itinance avatar Jul 26 '17 17:07 itinance

@itinance -- read the above a couple of times. I don't see anything wrong there. One thing I can think of is that if something went wrong in previous upsert, did the same filtering work using a tool like MySQL Workbench?

Another is, if I read it correctly, "attachment_processed": 0returned none and any other values (positive integer) returned all records. Sounds like it was treated as boolean, but since the repro case has been lost, I propose we close this issue, keep it in mind and when the same occurs, can you pls file an issue again?

Setogit avatar Jul 31 '17 20:07 Setogit

Hi @Setogit! Thx for reaching out and thank you very much for your help. You're apparently right in the assumption that it could have something to do with a previous "upsert".

Today we set up a complete new server with an empty database and we can't reproduce this any more while it still was reproducable with the old database at any time.

(As for your workbench-suggestion: we use MongoDB instead of Mysql, but, however, the old database was deleted today. But if i ever run into a similiar issue i will do these query with the mongo-api itself)

itinance avatar Jul 31 '17 20:07 itinance

Hi, I got similar problem. I have query involving filtering using foreign key, but no result came. While filter using other fields, it worked.

Probable fix: I changed MongoDB datasource options

"options": { "useNewUrlParser": true, "strictObjectIDCoercion": true }, As the guide at: https://loopback.io/doc/en/lb3/MongoDB-connector.html#strictobjectidcoercion-flag.

then it worked.

iwanbz avatar Dec 10 '18 00:12 iwanbz