Best way to filter

I’m looking for suggestions to improve my current workflow. My process is as follows:

  1. I receive an input item with a body.
  2. I perform a PostgreSQL SELECT query to retrieve a list of terms that will be used for filtering.
  3. I use an “if” condition to filter out any message that contains any of the specified words.
  4. Finally, I output the valid messages.

My question is: if step 2 returns multiple items, should I merge or combine all the term bodies into a single filter before applying step 3? or is there other way to do it. as of now I merge combine all the items then do my if but that seem really clunky.

so is there a way to check “if contain in the list” vs “if contains in string” ?

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

I think what you have works fine, because there should be a balance between complexity to do the right thing vs ease of maintenance. I mean that if you have set something up in a way that works and is allowing you to comfortably maintain it, that should be fine. But if you absolutely want to make some changes, maybe the code block could help you.

The code block can allow you to filter in one go. Here’s an example where the input array in the JavaScript code block is the input item you mention that you receive with your body.

The HTTP request is just a simulation of your SELECT query. I don’t know what sort of data is being returned by your query, but I’d like to assume it’s an array (which is what I tried to demonstrate in my example).

In the code block, I used the following code:

const input =  ["testing this", "message 2", "hello world"]
const textToExclude = $input.all().map(i => i.json)
return {
  filteredItems: input.filter(i => !textToExclude.some(t => i.includes(t)))
}

Explanation:

  • input: as mentioned above that’s your body that you receive)
  • textToExclude: from the output of the HTTP request, I’m generating an array of items that I wish to exclude. Make it easier to process.
  • return the data: execute the filter() method on the input array to remove all entries that contain any of the items from the textToExclude array.
1 Like

Thanks for your input. Yes my stuff works but I’m wondering if there is a better way to do this type of filtering. Let me play with your code to see if that could be the answer.

Maybe there is a better way to do this. When you perform the PostgreSQL SELECT, you could add a field (implement the logic of if contain in the list ) in your sql. Then you could use it directly.

2 Likes

hmm I did not think about that one? I need to explore if I can do a “Select all keywords that are in $json.body”. let me explore that too.
Thank you !

I ended up preferring your approach. no code to maintain and directly link with the DB by executing a custom select query with the postgre node. thanks

excellent job

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.