Filtering results on server

Hi,

I am trying to get the child and parent record of a document. This is the scenario that I am trying to achieve.

Let’s say that I have a document with the following structure:

Meta ID: DOC1

{
  "a": {
    "attra": "attra-value",
    "b": [
      {
        "attrb": "attrb-value",
        "c": [
          {
            "attrc": "attrc-value",
            "d": [
              {
                "attrd": "attrd-value",
                "e": [
                  {
                    "attr1": "attr1 - value1",
                    "attr2": "attr2 - value1",
                    "attr3": "attr3 - value1"
                  },
                  {
                    "attr1": "attr1 - value2",
                    "attr2": "attr2 - value2",
                    "attr3": "attr3 - value2"
                  },
                  {
                    "attr1": "attr1 - value3",
                    "attr2": "attr2 - value3",
                    "attr3": "attr3 - value3"
                  }
                ],
                "f": {
                  "g": [
                    {
                      "gattr1": "attr1 - value1",
                      "gattr2": "attr2 - value1",
                      "gattr3": "attr3 - value1"
                    },
                    {
                      "gattr1": "attr1 - value2",
                      "gattr2": "attr2 - value2",
                      "gattr3": "attr3 - value2"
                    },
                    {
                      "gattr1": "attr1 - value3",
                      "gattr2": "attr2 - value3",
                      "gattr3": "attr3 - value3"
                    }
                  ]
                }
              }
            ]
          }
        ]
      }
    ]
  }
}

Now I want the whole document but from array “e”, I just need the json object having the value of “attr1” and from “g” array I need the json object having the value of “gattr1”

This is what I need.

{
  "a": {
    "attra": "attra-value",
    "b": [
      {
        "attrb": "attrb-value",
        "c": [
          {
            "attrc": "attrc-value",
            "d": [
              {
                "attrd": "attrd-value",
                "e": [
                  {
                    "attr1": "attr1 - value1"
                  },
                  {
                    "attr1": "attr1 - value2"
                  },
                  {
                    "attr1": "attr1 - value3"
                  }
                ],
                "f": {
                  "g": [
                    {
                      "gattr1": "attr1 - value1"
                    },
                    {
                      "gattr1": "attr1 - value2"
                    },
                    {
                      "gattr1": "attr1 - value3"
                    }
                  ]
                }
              }
            ]
          }
        ]
      }
    ]
  }
}

How can I achieve this via sub doc api or n1ql for the meta id DOC1

Thanks,
Himanshu

@vsr1 Any inputs from your end?

Maybe something like this?

SELECT RAW ({"a":{
      "attra": a.attra,
      "b": ARRAY  {
          "attrb": b.attrb,
          "c": ARRAY {
              "attrc": c.attrc,
              "d": ARRAY {
                  "attrd": d.attrd,
                  "e": ARRAY {"attr1": e.attr1} FOR e IN d.e END,
                  "f": {
                    "g": ARRAY {"gattr1": g.gattr1} FOR g IN d.f.g END
                  }
                } FOR d IN c.d END
            } FOR c IN b.c END
        } FOR b IN a.b END
    }}) FROM tutorial USE KEYS "DOC1"