这主要是一个设计/效率问题,但我想看看在 Neo4j 中是否有一种首选方法来处理这个问题,而不是在 sql 数据库中如何处理。
现在我有 2 个模型 -user
and event
。我之间也有关系user
and event
表示他们将参加该活动。我想找出代表活动管理员的最佳方式。换句话说,我希望能够查询用户管理的事件。
一种方法是创建一种新的关系,称为admin_of
用户与之间event
。另一种方法是创建出席关系的管理属性。就像是admin: true
The admin_of
查询看起来很简单,但增加了与数据库的另一种关系。稍后它也能够处理多管理员。
我认为后一种方式可以通过类似的方式查询:(来自文档)EnrolledIn.where(since: 2002)
所以我的搜索将包括admin: true
。但是我不知道如何链接它,因为我希望它只是朋友事件。另一个where
查询通常基于节点而不是关系的属性。
后一种方法似乎是首选方法,但是查询它的正确方法是什么?或者第一种方法是否更简单,即使它增加了额外的关系?
Update:我想出了这样的东西
result = Event.query_as(:event).match("event<-[invite: INVITED]-(user: User)<-[:FRIENDS_WITH]-(user)").where(invite: {admin: /true/}).pluck(:event)
基于详细查询
https://github.com/neo4jrb/neo4j/wiki/Search-and-Match https://github.com/neo4jrb/neo4j/wiki/Search-and-Match
Update 2
我现在有这个,但没有匹配。开始剖析我的查询出了什么问题的最佳方法是什么?
current_user.friends.events.query_as(:event).match("event<-[invite]-(user: User)<-[friends_with]-(user)").where(invite: {admin: true}, event: {detail: 'property'}).pluck(:event)
我的用户模型有
has_many :both, :events, model_class: 'Event', rel_class: 'Invite'
我的事件模型有
has_many :both, :users, model_class: 'User', rel_class: 'Invite'
我的邀请模型有
from_class Event
to_class User
type 'invited'