cancel
Showing results for 
Search instead for 
Did you mean: 

Define "logical" property for entities

Andrea
Node

Hi all,

I would like to know if and how I can solve a problem.
Suppose to have a node (or a relationship) and you want to add a "virtual" property on it instead of persist it on db.
To keep it simple suppose you have a node n with two numerical property, say pA and pB, and you want to define a property pC as pA + pB.
Can a frontend application see pA, pB and pC simply with a return n?
Is it possible?

6 REPLIES 6

glilienfield
Ninja
Ninja

It sound like you just want the computed value just for the purpose of returning it calculated instead of the front-end doing the calculation. If this is true and you want to return all the properties as a map, instead of the entire node (the node includes the property map, list of labels, and id), then you could use the following type of query.

match (n)
with properties(n) as props, n.pA+n.pB as pC
return props{.*, pC}

This will return a map of all the node properties, with the addition of your calculated value. If you want a different key, then change it in the ‘with’ statement or change the return statement to

return props{.*, desiredKey: pC}

You can always add the node id and labels to the return statement if those are also desired.

If you really want to return a node, I think you can leverage the virtual nodes in the APOC library to create a virtual node with the properties you want plus your calculated property. This node can be returned in your query. Looks like you can pass the map above to this APOC method: props{.*, pC}.

I tested this. You could do the following:

match (n)
with properties(n) as props, n.pA+n.pB as pC
with props{.*, pC} as map
return apoc.create.vNode([‘label’], map) as node

Thanks Gary, I'm aware of virtual entities.
I need something different.
I would like this property with just a return n(ode).
I would like I'm able to define this property once for all and not have the needing to write a cypher query.
More or less I need something like the views in a relational database.

Thanks a lot!

I got it. I have used computed columns in oracle to do the same. I don't know of anything equivalent in neo4j. Could you use an APOC trigger to compute it every time these nodes with these properties are updated?

Thanks Gary!
I wasn't aware of APOC trigger till today!
Unfortunatly I need trigger "on read"...

glilienfield
Ninja
Ninja

I figured you could possibly use a trigger to calculate and store the computed value automatically each time these nodes with these properties are updated. As such, it will be there when you read these nodes. I know it is not ideal. You would have remediate the existing data too. I guess it may be easier to update the write query instead. Sorry I couldn’t help.

Andrea
Node

Thanks Gary!
I hope there's a solution out there
Otherwise I had to persist the property...