How to Store Ranges of Values?

I don't have a lot of experience with Neo4j and I am in the middle of building out a new database and I am not 100% certain the best way to model ranges of values. So I have nodes that have a lot of min/max values and I would like to be able to intuitively write queries that will show nodes that fall in some range or at least be able to group things together spatially using this data.

NodeA: min = 3, max = 10
NodeB: min=5, max=12
NodeC: min= 12, max=15

Query: Which Nodes have some value between 6 and 11?

Result: NodeA and NodeB

I feel like there is a way to do this using the Spatial data types but I am not certain. Or should I just go with setting the min, max as float properties and doing some type of greater than less than logic?


If the number of possible min/max values is small, you could break them out into nodes and do something like this:

MATCH (min:Value { value: 3 })-[:min]-(target:Node)-[:max]-(max:Value { value: 7 }) 
RETURN target

This will be better if you have a very large number of target nodes, and a relatively small number of possible Values which could be a minimum or a maximum.

If min and max could be any integer or float, and you could have zillions of them, then you'll be better off putting a min and max property on each node, indexing those, and using the <, > logic you suggest.

Thanks for the quick reply!
I think I'm gonna have to go with the <> logic.

1 Like

Or just use two properties like SET n.minValue=3, n.maxValue=5
Or an array with two entries: SET n.value: [3,5]

1 Like