我正在使用这个密码来检索一组库的嵌套数据,每个库都有0-X个书籍,每本书都有0-X个作者。密码看起来像这样:
MATCH (library:Library)--(book:Book)--(author:Author)
WITH library, book, {name: author.name} AS nest_author
WITH library, {name: book.name, authors: collect(nest_author)} AS nest_book
WITH {name: library.name, books: collect(nest_book)} AS nest_library
RETURN {library_data: collect(nest_library)}
这个密码的唯一问题是,只有当图书馆有书并且图书有作者时才有效。我尝试了下面的查询,但结构丢失,所有作者都出现在所有书籍下,所有书籍出现在所有图书馆下面:
MATCH (library:Library),(book:Book),(author:Author)
OPTIONAL MATCH (library)--(book)
OPTIONAL MATCH (book)--(author)
WITH library, book, {name: author.name} AS nest_author
WITH library, {name: book.name, authors: collect(nest_author)} AS nest_book
WITH {name: library.name, books: collect(nest_book)} AS nest_library
RETURN {library_data: collect(nest_library)}
我如何编写第一个查询,以便关系是可选的,当库没有任何书籍或书籍不包含作者时,收集只返回一个空列表?
答案 0 :(得分:2)
在您的第二个查询中,您正在创建跨产品行为。
基于以下neo4j控制台示例:http://console.neo4j.org/r/l1cpwz
此查询可以解决问题:
MATCH (n:Library)
OPTIONAL MATCH (n)--(book:Book)
OPTIONAL MATCH (book)--(author:Author)
RETURN n.name, collect({ book: book.title, author: author.lastname })
示例结果如下:
n.name collect({ book: book.title, author: author.lastname })
aspernatur [ {book:"Eligendi fuga est harum est nobis nam id.", author:null}, {book:"Numquam animi ex alias rerum quas.", author:null}]
consectetur [ {book:"Vel nostrum modi officia.", author:null}, {book:"Dolorum dolor optio aut ipsum.", author:null}, {book:"Iste sit quo reprehenderit dolores neque.", author:null}]
corrupti [ {book:"Minus sit sed quia tempora quaerat.", author:null}]
deserunt [ {book:"Temporibus impedit quod aut vel et.", author:null}, {book:"Praesentium perspiciatis blanditiis et omnis suscipit nostrum repudiandae.", author:null}]
labore [ {book:"Non quis maxime doloremque et.", author:null}, {book:"Eos voluptatem voluptatibus mollitia exercitationem nobis.", author:null}, {book:"Vel voluptatem quae libero non est eaque.", author:null}, {book:"Atque velit accusantium perspiciatis assumenda voluptas ea corporis.", author:null}]
nihil [ {book:"Deleniti illo earum eaque sapiente perspiciatis omnis quia.", author:null}, {book:"Est eum enim quo suscipit et molestias.", author:null}, {book:"Consequuntur nam temporibus explicabo quia ipsum.", author:null}, {book:"Facilis ratione illum ipsum sed cum ut.", author:null}, {book:"Est eos quis suscipit ratione alias necessitatibus asperiores sunt.", author:null}, {book:"Odit voluptatem est reiciendis quod tempora.", author:null}]
officiis [ {book:"Ad libero porro quibusdam voluptatum.", author:null}, {book:"Numquam omnis ut unde sequi sint enim.", author:null}]
omnis [ {book:"Qui laboriosam alias repellendus quibusdam incidunt et.", author:null}, {book:"Eos officia vitae consequatur qui.", author:null}, {book:"Explicabo nemo eos maxime.", author:null}, {book:"Dolores voluptatum et similique natus dolore fuga perferendis molestiae.", author:null}, {book:"Quidem aut ut ut quas veritatis.", author:null}, {book:"In in quos et aspernatur dolores.", author:null}, {book:"Alias sed quasi explicabo possimus veniam temporibus.", author:null}, {book:"Omnis dolor eum molestiae veritatis.", author:null}]
tempore [ {book:null, author:null}]