Grafana, Mongodb. If variable is empty how to omit it in mongodb query?

Viewed 47

I have a problem to solve in Grafana. We have a Grafana dashboard with multiple variables used in panels. It can happen that all variables are non-null, then the query works all find but, it can happen that we want to omit one or two variables but, then the MongoDB query is not going to work because we don't pass variables to the query that exists in mongo collection.

Example:

default.nfts_timeframes.aggregate(
  [
    {$match: {
      timeframe: "day",
      "nft_type": "$nftType"
     }
    },
    {$group: {
      _id: {
        "timestamp": "$timestamp",
        "nft_hash": "$nft_hash",
        "nft_type": "$nft_type"
       },
      volume: {$sum: "$volume"},
      buy: {$sum: "$buy"}
     }
    }
  ]
)

The "$nftHash" can be empty so now the query is not going to work. But I want mongo to omit this variable and query without it, so basically it returns all documents that match the other requirements.

Do you have any idea how to go about solving this? Is there any possibility to solve this with som if else statements inside the grafana that if $nftHash is empty i use one type of query if is not empty i use other. Or can i somehow solve it in mongodb query operation?

0 Answers
Related