Firestore JSON Where Query "And-Condition"

Describe the issue/error/question

Hi all,

I am currently switching from Make to n8n and so far I am impressed and it seems like it could be a good decision. However, I am struggling with one node in the last days… Doing a Firestory query with and condition. This was something that could be setup very easily on Make, but I guess my skills are just not enough, so maybe someone could help me easily. This is the JSON query I am using to find documents in my Firestore collection. However, based on the where query there can be multiple results (this is expected), that’s why I had two where queries setup in Make. And idea of how I can add up the and condition for a second where query in the same query so I can query in field2 with another value?

{
“structuredQuery”:{
“where”:{
“fieldFilter”:{
“field”: {
“fieldPath”:“field1”
},
“op”:“EQUAL”,
“value”: {
“stringValue”: “{{ $json[“to”][“value”][0][“address”] }}”
}
}
},
“from”:[
{
“collectionId”:“collection1”
}
]
}
}

What is the error message (if any)?

Please share the workflow

(Select the nodes and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow respectively)

Share the output returned by the last node

Information on your n8n setup

  • n8n version:
  • Database you’re using (default: SQLite):
  • Running n8n with the execution process [own(default), main]:
  • Running n8n via [Docker, npm, n8n.cloud, desktop app]:
1 Like

Hi @Userply, welcome to the community :tada:

You can use Firestore’s CompositeFilter for the job. If you are searching for a document with first_name = Patrick and last_name = Star, the JSON query could look like below.

Document:

image

Query:

{
  "structuredQuery": {
    "where": {
      "compositeFilter": {
        "op": "AND",
        "filters": [{
          "fieldFilter": {
            "field": {
              "fieldPath": "first_name"
            },
            "op": "EQUAL",
            "value": {
              "stringValue": "Patrick"
            }
          }
        }, {
          "fieldFilter": {
            "field": {
              "fieldPath": "last_name"
            },
            "op": "EQUAL",
            "value": {
              "stringValue": "Star"
            }
          }
        }]
      }
    },
    "from": [{
      "collectionId": "demo"
    }]
  }
}

In an n8n workflow using expressions this could look like so:

This is the result:

Hope this helps!

1 Like

Hi @MutedJam,

Thanks so much for your response. That’s exactly what I was looking for and I can’t thank you enough :slight_smile:

All the best!

1 Like

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