4
votes

I'm working on a project in flask and neo4j. I need to retrieve all the properties from a node as dict. Something like this

{'gender': 'male', 'password': '$2a$12$fd5KtsMjZHz26goBGcF3/.gZhZUP/6YAP7lRQ8Kf6eB5m69EhB5lS', 'email': '[email protected]', 'age': '50', 'country': 'US', 'username': 'xyz'}

I stumbled across this question while searching for answer

How can I return all properties for a node using Cypher? where it is sugegsted that it is possible to return properties names as keys.

In version 2.3.0, it is possible return values as well For e.g I have node with these properties

username xyz

email [email protected]

age 50

gender male

password $2a$12$fd5KtsMjZHz26goBGcF3/.gZhZUP/6YAP7lRQ8Kf6eB5m69EhB5lS

If I return n with query below

>>>for record in graph.cypher.execute("MATCH (n:User) WHERE n.username='xyz' RETURN n"):

...         print(record[0])

Results are returned in a row with this in front (n11:User, so I cannot use this result in jinja template directly without further processing


(n11:User {age:"50",country:"US",email:"[email protected]",gender:"male",password:"$2a$12$fd5KtsMjZHz26goBGcF3/.gZhZUP/6YAP7lRQ8Kf6eB5m69EhB5lS",username:"xyz"})

If I use the query below

>>>for record in graph.cypher.execute("MATCH (n:User) WHERE n.username='xyz'
RETURN EXTRACT(key IN keys(n) | {value: n[key], key:key})"):

...         print(record[0])

I get these results.

[{'value': '[email protected]', 'key': 'email'}, {'value': '50', 'key': 'age'}, {'value': 'US', 'key': 'country'}, {'value': 'xyz', 'key': 'username'}, {'value': '$2a$12$fd5KtsMjZHz26goBGcF3/.gZhZUP/6YAP7lRQ8Kf6eB5m69EhB5lS', 'key': 'password'}, {'value': 'male', 'key': 'gender'}]

Problem with this query is that, it doesn't really return key value tuples but instead append key and value labels in front of keys and values. It is not possible to use the output as it without further processing.
Or is there another way to run the query to get results as dict ?

Also, from idea box for Neo4j

https://trello.com/c/FciCdgWl/7-cypher-property-container-functions

It seems it could be possible to do these queries

Possible functions:

MATCH n RETURN keys(n) Returns the collection of property keys.

MATCH n RETURN values(n) Returns the collection of property values.

MATCH n RETURN entries(n) Returns a collection of key/value pairs.

But I can only run - MATCH n RETURN keys(n)

For rest I get invalid syntax error. Rest of the function are not implemented ?

2

2 Answers

8
votes

If you just do RETURN n then the node properties are returned as a map.

See: http://neo4j.com/docs/stable/rest-api-transactional.html#rest-api-execute-multiple-statements

This one controls you programmatically filter the properties that you want to return:

MATCH (n) WHERE id(n)=#
RETURN EXTRACT(key IN keys(n) | {key: key, value: n[key]}) 

Otherwise if you know which ones to return you spell them out: RETURN n.name, n.age.

0
votes

According to the cypher documentation for neo4j 3.5, there is an "all-properties selector" for maps. You can also use the properties function mentioned here:

MATCH (node)
RETURN
  id(node) AS neo4j_id,
  node {.*} AS node_properties_1,
  properties(node) AS node_properties_2
LIMIT 2