0

I have this document:

{
    "artifact": {
        "id": 1,
        "tags" : [
            "art",
            "art2"
        ],
        "origins": [
            {
                "id": 2,
                "tags" : [
                    "o1",
                    "o11"
                ],
                "origins": [
                    {
                        "id": 3,
                        "tags" : [
                            "o2",
                            "o3"
                        ]
                    }
                ]
            }
        ]
    },
    "insertionDate": "15022023095425"
}

I need to write a query to check if an id matches the id of the deepest origins object of another document.

At first, I thought something like this would work:

common_elements = db.collection("tasks").where("artifact.origins.origins.id", "==", current_item_id)

Is it possible to query object properties in a nested list with Firestore?

If this is not possible, how should I remodel my data? Could I store origins objects in another collection and reference them in this document?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
lucas24007
  • 93
  • 1
  • 6

1 Answers1

2

I need to write a query to check if an id matches the id of the deepest origins object of another document.

Using your actual database schema, you cannot perform such a query. If you only need to filter documents from the tasks collection by the deepest ID, then you should consider creating a non-nested field inside the document called deepestId, which in your example should contain the value of 3 and perform the following query:

common_elements = db.collection("tasks").where("deepestId", "==", 3)

If such a query can return multiple results, then you should add another where() call, to filter the documents based on the deepestId and the id like this:

common_elements = db.collection("tasks").where("deepestId", "==", 3).where("id", "==", 1)

Another possible solution would be to duplicate the data by creating another collection that should contain only the tasks with the deepestId == 3.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Hey Lucas. Have you tried my solution above, does it work? – Alex Mamo Feb 17 '23 at 08:58
  • 1
    Thanks for your response Alex. I decided to store all origins object in a different collection. I replace the origins objects within origins container with their ids. Doing so I can browse the origins "tree". – lucas24007 Mar 02 '23 at 10:27