Query in nested objects












0















have follow graph



CREATE (s1:SOME {id:'s1'} )  
CREATE (s2:SOME {id:'s2'} )
CREATE (a11:ANOTHER {id:'a11', tags:["tag1", "tag2"] } )
CREATE (a12:ANOTHER {id:'a12', tags:["tag2", "tag3"] } )

CREATE (a21:ANOTHER {id:'a21', tags:["tag3", "tag4"] } )
CREATE (a22:ANOTHER {id:'a22', tags:["tag4", "tag5"] } )

CREATE (s1)-[:CONTAINS ]->(a11),
(s1)-[:CONTAINS ]->(a12),
(s2)-[:CONTAINS ]->(a21),
(s2)-[:CONTAINS ]->(a22)


and populate records it with follow query:



match (s:SOME)
with s
OPTIONAL MATCH (a:ANOTHER)-[r:CONTAINS]-(s)
WITH s, COLLECT(DISTINCT a) AS aa

return s.id, aa


now I want to add some restrictions on ANOTHER.tags field:



match (s:SOME)
with s
OPTIONAL MATCH (a:ANOTHER)-[r:CONTAINS]-(s)
where 'tag2' in a.tags
WITH s, COLLECT(DISTINCT a) AS aa

return s.id, aa


result:



╒══════╤══════════════════════════════════════════════════════════════════════╕
│"s.id"│"aa" │
╞══════╪══════════════════════════════════════════════════════════════════════╡
│"s2" │ │
├──────┼──────────────────────────────────────────────────────────────────────┤
│"s1" │[{"id":"a11","tags":["tag1","tag2"]},{"id":"a12","tags":["tag2","tag3"│
│ │]}] │
└──────┴──────────────────────────────────────────────────────────────────────┘


But I want to skip s2 record at all, since it not contains required tag



another thing - what If I want to check multiple tags ? like ["tag1", "tag2"] (should respond with empty data in this case ) can I use something like intersection?










share|improve this question























  • Does the following snippet work for you? WITH aa WHERE size(aa) > 0 or WITH DISTINCT a WHERE a IS NOT NULL, alternatively have a look at the filter function [neo4j.com/docs/developer-manual/3.4/cypher/functions/list/….

    – ThirstForKnowledge
    Nov 22 '18 at 18:08
















0















have follow graph



CREATE (s1:SOME {id:'s1'} )  
CREATE (s2:SOME {id:'s2'} )
CREATE (a11:ANOTHER {id:'a11', tags:["tag1", "tag2"] } )
CREATE (a12:ANOTHER {id:'a12', tags:["tag2", "tag3"] } )

CREATE (a21:ANOTHER {id:'a21', tags:["tag3", "tag4"] } )
CREATE (a22:ANOTHER {id:'a22', tags:["tag4", "tag5"] } )

CREATE (s1)-[:CONTAINS ]->(a11),
(s1)-[:CONTAINS ]->(a12),
(s2)-[:CONTAINS ]->(a21),
(s2)-[:CONTAINS ]->(a22)


and populate records it with follow query:



match (s:SOME)
with s
OPTIONAL MATCH (a:ANOTHER)-[r:CONTAINS]-(s)
WITH s, COLLECT(DISTINCT a) AS aa

return s.id, aa


now I want to add some restrictions on ANOTHER.tags field:



match (s:SOME)
with s
OPTIONAL MATCH (a:ANOTHER)-[r:CONTAINS]-(s)
where 'tag2' in a.tags
WITH s, COLLECT(DISTINCT a) AS aa

return s.id, aa


result:



╒══════╤══════════════════════════════════════════════════════════════════════╕
│"s.id"│"aa" │
╞══════╪══════════════════════════════════════════════════════════════════════╡
│"s2" │ │
├──────┼──────────────────────────────────────────────────────────────────────┤
│"s1" │[{"id":"a11","tags":["tag1","tag2"]},{"id":"a12","tags":["tag2","tag3"│
│ │]}] │
└──────┴──────────────────────────────────────────────────────────────────────┘


But I want to skip s2 record at all, since it not contains required tag



another thing - what If I want to check multiple tags ? like ["tag1", "tag2"] (should respond with empty data in this case ) can I use something like intersection?










share|improve this question























  • Does the following snippet work for you? WITH aa WHERE size(aa) > 0 or WITH DISTINCT a WHERE a IS NOT NULL, alternatively have a look at the filter function [neo4j.com/docs/developer-manual/3.4/cypher/functions/list/….

    – ThirstForKnowledge
    Nov 22 '18 at 18:08














0












0








0








have follow graph



CREATE (s1:SOME {id:'s1'} )  
CREATE (s2:SOME {id:'s2'} )
CREATE (a11:ANOTHER {id:'a11', tags:["tag1", "tag2"] } )
CREATE (a12:ANOTHER {id:'a12', tags:["tag2", "tag3"] } )

CREATE (a21:ANOTHER {id:'a21', tags:["tag3", "tag4"] } )
CREATE (a22:ANOTHER {id:'a22', tags:["tag4", "tag5"] } )

CREATE (s1)-[:CONTAINS ]->(a11),
(s1)-[:CONTAINS ]->(a12),
(s2)-[:CONTAINS ]->(a21),
(s2)-[:CONTAINS ]->(a22)


and populate records it with follow query:



match (s:SOME)
with s
OPTIONAL MATCH (a:ANOTHER)-[r:CONTAINS]-(s)
WITH s, COLLECT(DISTINCT a) AS aa

return s.id, aa


now I want to add some restrictions on ANOTHER.tags field:



match (s:SOME)
with s
OPTIONAL MATCH (a:ANOTHER)-[r:CONTAINS]-(s)
where 'tag2' in a.tags
WITH s, COLLECT(DISTINCT a) AS aa

return s.id, aa


result:



╒══════╤══════════════════════════════════════════════════════════════════════╕
│"s.id"│"aa" │
╞══════╪══════════════════════════════════════════════════════════════════════╡
│"s2" │ │
├──────┼──────────────────────────────────────────────────────────────────────┤
│"s1" │[{"id":"a11","tags":["tag1","tag2"]},{"id":"a12","tags":["tag2","tag3"│
│ │]}] │
└──────┴──────────────────────────────────────────────────────────────────────┘


But I want to skip s2 record at all, since it not contains required tag



another thing - what If I want to check multiple tags ? like ["tag1", "tag2"] (should respond with empty data in this case ) can I use something like intersection?










share|improve this question














have follow graph



CREATE (s1:SOME {id:'s1'} )  
CREATE (s2:SOME {id:'s2'} )
CREATE (a11:ANOTHER {id:'a11', tags:["tag1", "tag2"] } )
CREATE (a12:ANOTHER {id:'a12', tags:["tag2", "tag3"] } )

CREATE (a21:ANOTHER {id:'a21', tags:["tag3", "tag4"] } )
CREATE (a22:ANOTHER {id:'a22', tags:["tag4", "tag5"] } )

CREATE (s1)-[:CONTAINS ]->(a11),
(s1)-[:CONTAINS ]->(a12),
(s2)-[:CONTAINS ]->(a21),
(s2)-[:CONTAINS ]->(a22)


and populate records it with follow query:



match (s:SOME)
with s
OPTIONAL MATCH (a:ANOTHER)-[r:CONTAINS]-(s)
WITH s, COLLECT(DISTINCT a) AS aa

return s.id, aa


now I want to add some restrictions on ANOTHER.tags field:



match (s:SOME)
with s
OPTIONAL MATCH (a:ANOTHER)-[r:CONTAINS]-(s)
where 'tag2' in a.tags
WITH s, COLLECT(DISTINCT a) AS aa

return s.id, aa


result:



╒══════╤══════════════════════════════════════════════════════════════════════╕
│"s.id"│"aa" │
╞══════╪══════════════════════════════════════════════════════════════════════╡
│"s2" │ │
├──────┼──────────────────────────────────────────────────────────────────────┤
│"s1" │[{"id":"a11","tags":["tag1","tag2"]},{"id":"a12","tags":["tag2","tag3"│
│ │]}] │
└──────┴──────────────────────────────────────────────────────────────────────┘


But I want to skip s2 record at all, since it not contains required tag



another thing - what If I want to check multiple tags ? like ["tag1", "tag2"] (should respond with empty data in this case ) can I use something like intersection?







neo4j cypher






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 16:49









Alexander PopovAlexander Popov

12711




12711













  • Does the following snippet work for you? WITH aa WHERE size(aa) > 0 or WITH DISTINCT a WHERE a IS NOT NULL, alternatively have a look at the filter function [neo4j.com/docs/developer-manual/3.4/cypher/functions/list/….

    – ThirstForKnowledge
    Nov 22 '18 at 18:08



















  • Does the following snippet work for you? WITH aa WHERE size(aa) > 0 or WITH DISTINCT a WHERE a IS NOT NULL, alternatively have a look at the filter function [neo4j.com/docs/developer-manual/3.4/cypher/functions/list/….

    – ThirstForKnowledge
    Nov 22 '18 at 18:08

















Does the following snippet work for you? WITH aa WHERE size(aa) > 0 or WITH DISTINCT a WHERE a IS NOT NULL, alternatively have a look at the filter function [neo4j.com/docs/developer-manual/3.4/cypher/functions/list/….

– ThirstForKnowledge
Nov 22 '18 at 18:08





Does the following snippet work for you? WITH aa WHERE size(aa) > 0 or WITH DISTINCT a WHERE a IS NOT NULL, alternatively have a look at the filter function [neo4j.com/docs/developer-manual/3.4/cypher/functions/list/….

– ThirstForKnowledge
Nov 22 '18 at 18:08












1 Answer
1






active

oldest

votes


















0














You just have to switch from OPTIONAL MATCH to MATCH only and it should skip null values.



We can also shorten the query by using only a single MATCH instead of two:



match (s:SOME)<-[r:CONTAINS]-(s)
where 'tag2' in a.tags
WITH s, COLLECT(DISTINCT a) AS aa

return s.id, aa





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435348%2fquery-in-nested-objects%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You just have to switch from OPTIONAL MATCH to MATCH only and it should skip null values.



    We can also shorten the query by using only a single MATCH instead of two:



    match (s:SOME)<-[r:CONTAINS]-(s)
    where 'tag2' in a.tags
    WITH s, COLLECT(DISTINCT a) AS aa

    return s.id, aa





    share|improve this answer




























      0














      You just have to switch from OPTIONAL MATCH to MATCH only and it should skip null values.



      We can also shorten the query by using only a single MATCH instead of two:



      match (s:SOME)<-[r:CONTAINS]-(s)
      where 'tag2' in a.tags
      WITH s, COLLECT(DISTINCT a) AS aa

      return s.id, aa





      share|improve this answer


























        0












        0








        0







        You just have to switch from OPTIONAL MATCH to MATCH only and it should skip null values.



        We can also shorten the query by using only a single MATCH instead of two:



        match (s:SOME)<-[r:CONTAINS]-(s)
        where 'tag2' in a.tags
        WITH s, COLLECT(DISTINCT a) AS aa

        return s.id, aa





        share|improve this answer













        You just have to switch from OPTIONAL MATCH to MATCH only and it should skip null values.



        We can also shorten the query by using only a single MATCH instead of two:



        match (s:SOME)<-[r:CONTAINS]-(s)
        where 'tag2' in a.tags
        WITH s, COLLECT(DISTINCT a) AS aa

        return s.id, aa






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 20:01









        Tomaž BrataničTomaž Bratanič

        1,9002718




        1,9002718
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435348%2fquery-in-nested-objects%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Feedback on college project

            Futebolista

            Albești (Vaslui)