考虑下面的图形数据模型。这些日子在一个链接列表中连接,但它们包含时间戳。如果我想收集Stats
一个范围之间的节点,我必须首先选择那些日期节点,然后我可以选择Stats
紫色的节点。从那里我可以指定那些紫色节点必须连接到连接到我指定Group
的黄色节点。Location
现在,如果我将此模式翻译成 Cypher,我会得到以下信息:
MATCH (d:Day)
WHERE d.timestamp > 123456789 AND d.timestamp < 234567891
MATCH (topic:Topic), (location:Location { city: "San Francisco" })
WHERE topic.name in ["NoSQL"]
WITH topic, location, day
MATCH (topic)<-[:HAS_TOPIC]-(group:Group)-[:LOCATED_IN]->(location)
WITH DISTINCT group, day
MATCH (group)-[:HAS_MEMBERS]->(stats:Stats)-[:ON_DAY]->(day)
WITH DISTINCT (day.month + "/" + day.day + "/" + day.year) as day,
group.name as group,
stats.count as members,
day.timestamp as timestamp
ORDER BY timestamp
RETURN day, group, members
如果您重构模型以将in_stock
关系转换为具有时间戳的节点,并将该节点建模为链表,那么您可以通过指定模式来选择最新的:
MATCH (product:Product { sku: 1234 })-[:HAS_UPDATE]->(update:InStock)
WHERE NOT (update)-[:NEXT]->()
WITH update
MATCH (update)-[:STOCK_ITEMS]->(stockItems:StockItem),
(stockItems)<-[:STORED_AT]-(location:Location)
RETURN location.name, stockItems.quantity
这是执行此操作的最高效方式。管理链表中的指针,使您既可以查询范围(在时间戳之间),也可以查询最近的 N 个项目。