Return node and omit certain properties

Hi everyone!

I want to return a list of nodes but hide certain properties from being returned. I cannot use role privileges in this case, so I somehow have to filter out the properties.

This query removes the key from each node:

// match something
MATCH (n) WHERE n.key = "something"
// expand node into separate variables and remove the key from the proeprties
WITH id(n) as identity, labels(n) as labels, apoc.map.removeKey(properties(n), 'key') AS properties
// combine node again
WITH { identity: identity, labels: labels, properties: properties } as node
// return list of nodes
RETURN collect(node) as nodes

Now I wanted to see if there's a native Cypher way of doing that, or maybe a custom APOC procedure that I don't know of? I know there are virtual nodes from apoc.create.vNode but they return negative ids which is not optimal for my use case.

What is missing from your current solution?

Use apoc.map.clean if you want to remove a list of keys in one call.

I'm using apoc.map.removeKeys to remove multiple keys at once. I guess it's basically the same?

My solution works, I just wanted to explore if there's cleaner or faster way to achieve the same?