Items_query complex filter (mix of AND/OR operators)

In the monday.com UI, I can filter items based on a combination of AND/OR criteria using the “Add new group” button.

How can we do this via the API? There doesn’t seem to be documentation on how to combine predicates in the items_page.query_params.rules object.

Is this not possible via the API? If it is possible, what would the query_params look like for a query like this one:

(Name=“Jim” OR Name=“Ana”) AND Age = 35

Thanks.

1 Like

Hi Antonio,

You can use items_page and use the operator field in the query_params when setting the rules, as said in the documentation here.

You can insert the operator inside a specific rule:
query_params: {rules: {column_id: "timeline", compare_value: ["2023-06-30", "2023-07-01"], compare_attribute: "START_DATE", operator:between}

You can also use the operator for rules themselves:
query_params: {rules: [{column_id: "people", compare_value: ["Person 1", "Person 2"], operator:contains_text} {column_id: "status", compare_value: [1, 0]}]operator:and}

Hope this helps with your queries!

Best,
Joseph

1 Like

Hi Joseph. Thanks for your response.

I’m still unclear as to how I can combine both logical operators (AND/OR) in an items_query.

For example, I can find projects that last less than 5 days OR more than 100 days (e.g. I’m looking for outliers) like so:

query_params:
{
rules: [
{
column_id: “duration__days_”,
compare_value: 5,
operator: lower_than
},
{
column_id: “duration__days_”,
compare_value: 100,
operator: greater_than
}
],
operator: or
}
)

How can I now add an extra criteria that says that the Status of the project is “Stuck”, i.e. find projects that are stuck, whose duration is either lower than 5 or higher than 100?

In pseudo-code this would be an expression like:

(DurationDays < 5 OR DurationDays > 100) AND Status = ‘Stuck’

Notice I’m combining the OR operator with the AND operator. In the UI I would do this using the “Add new group” button, but I don’t know the equivalent way to do this via the API.

Hi Antonio,

I think you can do something like this:

query_params:
{rules: [{[{column_id: “duration__days_”,compare_value: 5,operator: lower_than
},{column_id: “duration__days_”,compare_value: 100,operator: greater_than}],
operator: or}},
[{column_id:“status”,compare_value:“stuck”,operator:contains_text]operator: and}

Essentially, you can create the list of rules and use an operator between the list of rules.

Best,
Joseph

1 Like

Hi Joseph,

Nesting an array of rules inside a parent rule makes sense to me, for this requirement, but it doesn’t appear to work. I keep getting parse error and red squiggles in the API playground.

Could you give it a try on your end? I might be messing something up with parentheses.

I’d very much appreciate a working example query.

Thanks,
Antonio

Hi Antonio,

Can you share the query_params you have ended up with? I am guessing you’ve been playing around with it more than just the example I provided. I will try on my end as well.

Best,
Joseph

1 Like

Dear Antonio,

I’ve reviewed your question regarding filtering items on Monday.com via the API using “and” and “or” combination criteria, and I managed to find a solution that might be helpful to you.

After exploring different approaches, I was able to craft a query that utilizes nested rules within groups to combine “and” and “or” logic as follows:

{
  boards(ids: 1452653689) {
    groups(ids: ["new_group29179"]) {
      items_page(
        query_params: {
          groups: {
            rules: [
              { column_id: "numbers", compare_value: ["100"], operator: any_of }
            ],
            operator: and,
            groups: {
              rules: [
                { column_id: "name", compare_value: ["Task 1"], operator: any_of },
                { column_id: "name", compare_value: ["Task 3"], operator: any_of }
              ],
              operator: or
            }
          }
        }
      ) {
        cursor
        items {
          id
          name
          column_values(ids: ["name", "numbers"]) {
            id
            text
          }
        }
      }
    }
  }
}