Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return all the properties of a node with values

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 ?

like image 990
jas Avatar asked Jan 08 '16 00:01

jas


People also ask

How to return all nodes in Neo4j?

Return all elements When you want to return all nodes, relationships and paths found in a query, you can use the * symbol.

Which query is used to return the count of the relationship for a node?

Using count(*) to return the number of nodes The function count(*) can be used to return the number of nodes; for example, the number of nodes connected to some node n . The labels and age property of the start node n and the number of nodes related to n are returned.

What is unwind in Neo4j?

With UNWIND , you can transform any list back into individual rows. These lists can be parameters that were passed in, previously collect -ed result or other list expressions. One common usage of unwind is to create distinct lists. Another is to create data from parameter lists that are provided to the query.


1 Answers

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.

like image 160
Michael Hunger Avatar answered Oct 27 '22 08:10

Michael Hunger