skip to Main Content

Here I have two nodes with the property ‘name’ that contains the first name and last name of a person. I create the vertices as:

SELECT * FROM cypher('graph', $$
CREATE (n:Person {name : "Michael Stone", age : 20}), (m: Person {name : "Michael Douglas", age: 19})  
RETURN n, m
$$) AS (n agtype, m agtype);

I want to retrieve the nodes where the first name is Michael. How can it be done? Is there any clause such as "LIKE" (as in postgreqsl) in AGE?

3

Answers


  1. You can use regular expression matching to find a substring within a string. In your case, it would look like:

    SELECT * FROM cypher('graph', $$
        MATCH (v:Person)
        WHERE v.name =~ 'Michael.*'
        RETURN v
    $$) AS (v agtype);
    

    Note that the ‘.’ operator is a wildcard and matches against any possible character, while the ‘*’ operator matches with 0 or more of the previous character (in this case it can be 0 or more of any possible character).

    Login or Signup to reply.
  2. I think you can use like operator like this

    MATCH (p:Person)
    WHERE p.name LIKE 'Michael%'
    RETURN p
    

    To get all the names starting with Michael as first name.

    Login or Signup to reply.
  3. One of the way to retrieve the nodes where the first name is Michael, is through following query.

    SELECT * FROM cypher('graph', $$
    MATCH (n:Person)
    WHERE split(n.name, ' ')[0] = 'Michael'
    RETURN n
    $$) AS (n agtype);
    

    Here in the query the MATCH clause is used to find all the node labeled as ‘Person’, WHERE clause is used to filter the results based on the first name of the person then the split function is called to split the name property based on the space delimiter and retrieve the first element of the resulting array.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search