Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to check if an array property contains a certain value?

I have a graph with two vertices, and each contain a property named interests, which is an array of strings. I wanted to compare how many strings both arrays have in common and also if the array contains a certain string.

I have tried the following query, but it throws an error:

SELECT * FROM cypher('QuirkyMatch', $$
        MATCH (v:Person), (user:Person)
        WHERE user.name = 'Sarah' AND v.age > (user.age + 1) AND v.interests CONTAINS 'Art'
        RETURN v
$$) as (potential_match agtype);

ERROR:  agtype string values expected

Here is how I created them:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SELECT * FROM cypher('QuirkyMatch', $$
    CREATE (:Person {
        name: 'Alex',
        age: 27,
        occupation: 'Graphic Designer',
        interests: ['Art', 'Photography', 'Traveling', 'Indies Music'],
        weird_fact: 'I can hold up to 400 straws in my mouth. LOL.'
    }), 
    (:Person {
        name: 'Sarah',
        age: 25,
        occupation: 'Software Engineer',
        interests: ['Hiking', 'Board Games', 'Sci-Fi Movies', 'Dungeons & Dragons', 'Painting', 'Art'],
        weird_fact: 'I collect hot sauces and I have over 50 different ones :)'
    })
$$) as (v agtype);

>Solution :

The CONTAINS keyword is not valid for checking if an element exists in an array in Cypher. Instead, try using the IN keyword. Here’s an updated query that should work:

SELECT * FROM cypher('QuirkyMatch', $$
    MATCH (v:Person), (user:Person)
    WHERE user.name = 'Sarah' AND v.age > (user.age + 1) AND 'Art' IN v.interests
    RETURN v
$$) as (potential_match agtype);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading