Search code examples
attributesarangodbaql

ArangoDB: filter out document attributes in a collection query


I've got an ArangoDB collection containing a lot of different types of documents (that is with different sets of attributes).

I would like to make a select query and filter out some attributes, that is return all but a couple of predefined attributes of the selected documents.

For example, if this the selected subset:

{a:1, b:2, c:3, d:4, e:5, h:8}
{a:21, b:22, c:23, d:4, w:5, p:81}
{a:31, b:22, c:63, f:4, e:5, u:82}
{a:41, b:32, c:53, g:4, h:5, l:83}
{a:51, b:42, c:53, h:4, j:5, n:84}
{a:61, b:52, c:33, i:4, u:5, m:85}

I'd like to return all attributes but 'b', 'c', 'd':

{a:1,  e:5, h:8},
{a:21,  w:5, p:81}
{a:31,  f:4, e:5, u:82}
{a:41,  g:4, h:5, l:83}
{a:51,  h:4, j:5, n:84}
{a:61,  i:4, u:5, m:85}

What is the most efficient way to do this ?


Solution

  • You can use the UNSET AQL function to do this. The following AQL query returns all documents from the collection, but for each document unsets the mentioned attributes b, c, and d.

    FOR doc IN collection 
      RETURN UNSET(doc, ['b', 'c', 'd' ])
    

    The result of the query for the above data set would be (note that I also excluded the attributes _id, _key and _rev here for brevity):

    [ 
      { 
        "a" : 41, 
        "h" : 5, 
        "g" : 4, 
        "l" : 83 
      }, 
      { 
        "a" : 31, 
        "e" : 5, 
        "f" : 4, 
        "u" : 82 
      }, 
      { 
        "a" : 61, 
        "u" : 5, 
        "i" : 4, 
        "m" : 85 
      }, 
      { 
        "a" : 1, 
        "e" : 5, 
        "h" : 8 
      }, 
      { 
        "a" : 51, 
        "h" : 4, 
        "j" : 5, 
        "n" : 84 
      }, 
      { 
        "a" : 21, 
        "w" : 5, 
        "p" : 81 
      } 
    ]