question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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 !

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

5reactions
hakimiocommented, Mar 21, 2020

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}
        }]
    }]
}
0reactions
leo-jnesiscommented, Mar 23, 2020

Of course, my bad!

Thanks a lot

Read more comments on GitHub >

github_iconTop Results From Across the Web

Search Techniques: Combining search terms
How to combine search terms: Using AND, OR and NOT. The three most commonly used operators are AND, OR, NOT. These are known...
Read more >
Google Search Operators: The Complete List (42 Advanced ...
My aim here is to show that you can achieve almost anything with Google advanced operators if you know how to use and...
Read more >
Search Operators | GovInfo
A space is used to separate words or operators in a search query. In a simple query where operators are not used, spaces...
Read more >
Refine web searches - Google Search Help
Combine searches. Put " OR " between each search query. For example, marathon OR race . Search for a specific site.
Read more >
Advanced search options - Microsoft Support
Because OR is the operator with lowest precedence, enclose OR terms in parentheses when combined with other operators in a search.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found