Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Querying Firestore documents by nested key-value in an array field using Python #781

Open
Vicbi opened this issue Mar 30, 2024 · 1 comment

Comments

@Vicbi
Copy link

Vicbi commented Mar 30, 2024

Is your feature request related to a problem? Please describe.
I'm working with Firestore in Python and have a data query challenge I'm hoping to get some advice on.

My documents in Firestore are structured as follows:

{
  "key1": "value1",
  "key2": {
    "subKey1": [
      {
        "arrayKey1": "arrayValue1",
        "arrayKey2": "arrayValue2"
      },
      {
        "arrayKey3": "arrayValue3",
        "arrayKey4": "arrayValue4"
      }
    ],
    "subKey2": "subValue2"
  }
}

My goal is to query documents where the value of arrayKey1 within the subKey1 array matches a specific value (for example, target_arrayValue1), regardless of the arrayKey2 in the same dictionary.

Here's an example of what my query currently looks like:

docs = query.where(
    filter=FieldFilter(
        "key2.subKey1",
        "array_contains",
        {
            "arrayKey1": "target_arrayValue1",
            "arrayKey2": "arrayValue2"
        }
    )
).stream()

This approach, however, requires a full match of the dictionary, which is not ideal.

Describe the solution you'd like
My “ideal” code would do something like this:

docs = query.where(
    filter=FieldFilter(
        "key2.subKey1.0.arrayKey1", 
        "==",
        "target_arrayValue1"
    )
).stream()

Is there an appropriate way to structure my query to achieve this? Any insights or examples would be greatly appreciated! Thanks in advance.

@google-oss-bot
Copy link

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants