Search code examples
graph-databasesarangodbaqlnosql

ArangoDB Query Attribute Embedded in List


I have a document embedded in a list, and I need to query for documents matching the value of a series of Strings.

My document:

enter image description here

As you can see, I have a regular document. Inside that document is "categories" which is of an unknown length per document. Inside categories I have "confidence" and "title". I need to query to find documents which have a titles matching a list of title I have in an ArrayList. The query I thought would work is:

FOR document IN documents FILTER document.categories.title IN @categories RETURN article

@categories is an ArrayList with a list of titles. If any of the titles in the ArrayList are in the document, I would like it to be returned.

This query seems to be returning null. I don't think it is getting down to the level of comparing the ArrayList to the "title" field in my document. I know I can access the "categories" list using [#] but I don't know how to search for the "title"s in "categories".


Solution

  • The query

    FOR document IN documents 
      FILTER document.categories.title IN @categories 
      RETURN article
    

    would work if document.categories is a scalar, but it will not work if document.categories is an array. The reason is that the array on the left-hand side of the IN operator will not be auto-expanded.

    To find the documents the query could be rewritten as follows:

    FOR document IN documents 
      FILTER document.categories[*].title IN @categories 
      RETURN document
    

    or

    FOR document IN documents 
      LET titles = (
        FOR category IN document.categories
          RETURN category.title
      )
      FILTER titles IN @categories 
      RETURN document
    

    Apart from that, article will be unknown unless there is a collection named article. It should probably read document or document.article.