Using Flow Variable in MongoDB Aggregation

Dear KNIME community,

I have a “MongoDB Aggregation” where I’m currently hardcoded a few parameters in $match etc inside the pipeline. If there’s any changes on the parameters, I need to modify their values inside the pipeline, which is quite tedious.

I come across “Variable Creator” and “String Configuration” components in KNIME so I thought maybe I can use that and pass the variables into MongoDB Aggregation through “Variable Inport”. However, it seems like I can’t use those variables in aggregation pipeline. Only can use them in:

  • Settings > Database
  • Settings > Collection, and
  • Flow Variables > pipeline

Appreciate if you guys can advice on how to use variable in the aggregation pipeline. Thanks in advance for your help!

Cheers
Cass

Hi @casslow,
The pipeline setting can be controlled via flow variable in the “Flow Variables” tab of the MongoDB Aggregation node’s configuration dialog. So you can first build the pipeline expression dynamically, e.g. using a String Manipulation (Variable) node, then give that to the MongoDB Aggregation node via String flow variable.
Kind regards,
Alexander

1 Like

Hi @AlexanderFillbrunn, thanks for the suggestion! It never come across my mind to do that. However, if I want to build the pipeline expression dynamically, it seems like the String Manipulation (Variable) node doesn’t support if-else logic (do correct me if I’m wrong).

Is there any other way to do that before going through String Manipulation (Variable) node and pass it to MongoDB Aggregation node?

Hi,
You might want to use the Column Expressions node. It allows you to write JavaScript with if-else switches and loops.
Kind regards,
Alexander

1 Like

Hi,
I see. I saw another node, Variable Expressions. It seems like similar to Column Expressions too. I’ll try on both and see which one suits my use case.

Thanks, @AlexanderFillbrunn!

1 Like

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