Crud: Combined $and and $or in search request generates only AND operators

Created on 13 Mar 2020  路  5Comments  路  Source: nestjsx/crud

Hi,

When I query this :

{
  "$and": [
    {
      "createdAt": {
        "$lte": "2020-03-13T15:49:37.289Z"
      }
    },
    {
      "$or": [
        {
          "name": {
            "$contL": "a"
          },
          "email": {
            "$contL": "a"
          }
        }
      ]
    }
  ]
}

I get this generated query :

SELECT * FROM "users" "User" WHERE "User"."createdAt" <= $1 AND ("User"."name" ILIKE $2 AND "User"."email" ILIKE $3) -- PARAMETERS: ["2020-03-13T15:50:59.592Z","%a%","%a%"]

I was expecting this :

SELECT * FROM "users" "User" WHERE "User"."createdAt" <= $1 AND ("User"."name" ILIKE $2 OR "User"."email" ILIKE $3) -- PARAMETERS: ["2020-03-13T15:50:59.592Z","%a%","%a%"]

am I doing something wrong in the search params ?

Thanks !

Most helpful comment

The issue is that nesting in the first post is not correct (or part is nested incorrectly). It should be:

{
    "$and": [{
        "createdAt": {
            "$lte": "2020-03-13T15:49:37.289Z"
        }
    }, {
        "$or": [{
            "name": {
                "$contL": "a"
            }
        }, {
            "email": {
                "$contL": "a"
            }
        }]
    }]
}

image

I've tested this type of nested conditions with the latest version of CRUD library and it works as expected for me.

To avoid this kind of issues in the future I suggest writing search conditions the following way:

{
    $and: [{
        name: {$excl: 'Second'}
    }, {
        $or: [{
            field1: {$lt: 4}
        }, {
            field2: {$gt: 6}
        }]
    }]
}

All 5 comments

Any thoughts on this ?

I also need to know this. In our case we are using a global filter and is messing our $or. If I just use this with $and filters it works OK but once I introduce an $or in a query param that messes the $and and $or combination. Any thoughts on this

another post that tries to come around this issue https://github.com/nestjsx/crud/issues/437

The issue is that nesting in the first post is not correct (or part is nested incorrectly). It should be:

{
    "$and": [{
        "createdAt": {
            "$lte": "2020-03-13T15:49:37.289Z"
        }
    }, {
        "$or": [{
            "name": {
                "$contL": "a"
            }
        }, {
            "email": {
                "$contL": "a"
            }
        }]
    }]
}

image

I've tested this type of nested conditions with the latest version of CRUD library and it works as expected for me.

To avoid this kind of issues in the future I suggest writing search conditions the following way:

{
    $and: [{
        name: {$excl: 'Second'}
    }, {
        $or: [{
            field1: {$lt: 4}
        }, {
            field2: {$gt: 6}
        }]
    }]
}

Of course, my bad!

Thanks a lot

Was this page helpful?
0 / 5 - 0 ratings

Related issues

josimarz picture josimarz  路  4Comments

tbrannam picture tbrannam  路  3Comments

Bnaya picture Bnaya  路  3Comments

rainercedric23 picture rainercedric23  路  3Comments

dulkith picture dulkith  路  3Comments