我一直试图创建一个节点并将其链接到其他节点的列表,我得到了以下查询:
MATCH (s:Subject), (p:Programme {name: 'Bsc. Agriculture' })
Where s.name IN ['Physics (CAPE)', 'Biology (CAPE)', 'Chemistry (CAPE)']
Create (c: Combo {amt:1}), (c)-[:contains]->(s), (p)-[:requires]->(c) return *
但遗憾的是,组合节点创建了三次,每个组合节点都链接到一个Subject节点。有什么方法可以调整此查询,以便只创建一个Combo节点吗?
答案 0 :(得分:1)
下面的查询使用aggregation function //bar//foo[count(./ancestor::quux) = count(./ancestor::bar/ancestor::quux)]
为class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
let cellId = "cellId"
override func viewDidLoad() {
super.viewDidLoad()
setupCV()
}
var containerCollectionView: UICollectionView = {
let layout = UICollectionViewLayout()
let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
cv.backgroundColor = UIColor.white
return cv
}()
}
extension ViewController {
fileprivate func setupCV() {
// these first two lines add the colelction view and apply constraints - they work and the CV is properly displayed inside the view controller
self.view.addSubview(containerCollectionView)
containerCollectionView.anchor(view.topAnchor, left: view.leftAnchor, bottom: view.bottomAnchor, right: view.rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
containerCollectionView.delegate = self
containerCollectionView.dataSource = self
containerCollectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: cellId)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 150, height: 150)
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath)
cell.backgroundColor = UIColor.yellow
return cell
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 3
}
}
生成一行数据(而不是三行),以确保只执行第一个COLLECT
每p
只有一次 - 因此每CREATE
只产生一个p
和Combo
关系。然后requires
子句创建所有必需的p
关系。
FOREACH
答案 1 :(得分:0)
您的查询为MATCH中找到的CREATE中使用的每个其他节点创建Combo节点。 其中一个解决方案可能是您先创建Combo节点,然后在查询的其他部分使用它
Create (c: Combo {amt:1})
WITH c
MATCH (s:Subject), (p:Programme {name: 'Bsc. Agriculture' })
Where s.name IN ['Physics (CAPE)', 'Biology (CAPE)', 'Chemistry (CAPE)']
CREATE (c)-[:contains]->(s), (p)-[:requires]->(c) return *
答案 2 :(得分:0)
如果使用MERGE而不是CREATE,它应该满足您的要求并避免创建重复的节点。但是这假设每个关系目标节点都由其所有唯一属性标识。试试这个......
MATCH (s:Subject), (p:Programme {name: 'Bsc. Agriculture' })
Where s.name IN ['Physics (CAPE)', 'Biology (CAPE)', 'Chemistry (CAPE)']
MERGE (c: Combo {amt:1}), (c)-[:contains]->(s), (p)-[:requires]->(c) return *
合并只有在元素不存在时才会创建元素。需要注意的是,有问题的元素必须与其所有参数(名称和属性)匹配。 https://graphaware.com/neo4j/2014/07/31/cypher-merge-explained.html还有更多内容。