mongodb where not exists (subtract two query results)










1















consider I have the following collection,



[


"user_id": 7,
"action": 1
,

"user_id": 8,
"action": 1
,

"user_id": 9,
"action": 1
,

"user_id": 7,
"action": 2


]


I need to find all users who has for example action 1 but not action 2.
In mysql It's possible to just select user_id with two queries and subtract this results, or use where not exists subquery.



how can I handle this with mongodb?



so my expected result is :



[8,9]









share|improve this question




























    1















    consider I have the following collection,



    [


    "user_id": 7,
    "action": 1
    ,

    "user_id": 8,
    "action": 1
    ,

    "user_id": 9,
    "action": 1
    ,

    "user_id": 7,
    "action": 2


    ]


    I need to find all users who has for example action 1 but not action 2.
    In mysql It's possible to just select user_id with two queries and subtract this results, or use where not exists subquery.



    how can I handle this with mongodb?



    so my expected result is :



    [8,9]









    share|improve this question


























      1












      1








      1








      consider I have the following collection,



      [


      "user_id": 7,
      "action": 1
      ,

      "user_id": 8,
      "action": 1
      ,

      "user_id": 9,
      "action": 1
      ,

      "user_id": 7,
      "action": 2


      ]


      I need to find all users who has for example action 1 but not action 2.
      In mysql It's possible to just select user_id with two queries and subtract this results, or use where not exists subquery.



      how can I handle this with mongodb?



      so my expected result is :



      [8,9]









      share|improve this question
















      consider I have the following collection,



      [


      "user_id": 7,
      "action": 1
      ,

      "user_id": 8,
      "action": 1
      ,

      "user_id": 9,
      "action": 1
      ,

      "user_id": 7,
      "action": 2


      ]


      I need to find all users who has for example action 1 but not action 2.
      In mysql It's possible to just select user_id with two queries and subtract this results, or use where not exists subquery.



      how can I handle this with mongodb?



      so my expected result is :



      [8,9]






      mongodb mongodb-query aggregation-framework






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 11:08









      Anthony Winzlet

      18.2k42345




      18.2k42345










      asked Nov 16 '18 at 9:06









      mhndevmhndev

      6581127




      6581127






















          2 Answers
          2






          active

          oldest

          votes


















          3














          You can use the aggregation pipeline to first group all actions into one array plus the associated user id into objects and the filter to have action 1, but not 2, then keep only the id:



          db.collection.aggregate([
          // group user id and all actions together

          $group:
          _id: "$user_id",
          actions:
          $addToSet: "$action"


          ,
          // filter documents which have 1 as action but not 2

          $match:
          $and: [

          "actions": 1
          ,

          "actions":
          $not:
          $eq: 2



          ]

          ,
          // only keep the id

          $group:
          _id: "$_id"


          ])


          This returns:



          [

          "_id": 8
          ,

          "_id": 9

          ]


          Here is a link to a playgorund: https://mongoplayground.net/p/So4HjEXx3sn



          You should consider how you structure your documents. Your design looks a bit like a relational database. It is advisable to model your documents according to your read accesses (if possible). In this case you could an user_id and an actions field, which has already all action ids grouped together.






          share|improve this answer























          • thanks for your good answer.

            – mhndev
            Nov 16 '18 at 10:17


















          2














          You can use $group and $push the actions for the distinct user_id. Finally use $match with actions $eq ual to 1 but not equal ($ne) to 2



          db.collection.aggregate([
          "$group":
          "_id": "$user_id",
          "actions": "$push": "$action"
          ,
          "$match": "actions": "$eq": 1, "$ne": 2 ,
          { "$project": "_id": 1
          ])





          share|improve this answer

























          • thanks, I thinks both answers are correct, so I chose the first answer.

            – mhndev
            Nov 16 '18 at 10:18






          • 1





            yes But you don't need to use two $group stages here. Simply use $project because we already find the distinct user_id in the first $group stage.

            – Anthony Winzlet
            Nov 16 '18 at 10:28











          • yes that's right

            – mhndev
            Nov 16 '18 at 10:45






          • 1





            I have updated the answer. Even that it has less operators. You don't need to use $group, $and, $not aggregation here.

            – Anthony Winzlet
            Nov 16 '18 at 17:10












          • yes, I noticed, thanks

            – mhndev
            Nov 16 '18 at 17:13











          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%2f53334567%2fmongodb-where-not-exists-subtract-two-query-results%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          You can use the aggregation pipeline to first group all actions into one array plus the associated user id into objects and the filter to have action 1, but not 2, then keep only the id:



          db.collection.aggregate([
          // group user id and all actions together

          $group:
          _id: "$user_id",
          actions:
          $addToSet: "$action"


          ,
          // filter documents which have 1 as action but not 2

          $match:
          $and: [

          "actions": 1
          ,

          "actions":
          $not:
          $eq: 2



          ]

          ,
          // only keep the id

          $group:
          _id: "$_id"


          ])


          This returns:



          [

          "_id": 8
          ,

          "_id": 9

          ]


          Here is a link to a playgorund: https://mongoplayground.net/p/So4HjEXx3sn



          You should consider how you structure your documents. Your design looks a bit like a relational database. It is advisable to model your documents according to your read accesses (if possible). In this case you could an user_id and an actions field, which has already all action ids grouped together.






          share|improve this answer























          • thanks for your good answer.

            – mhndev
            Nov 16 '18 at 10:17















          3














          You can use the aggregation pipeline to first group all actions into one array plus the associated user id into objects and the filter to have action 1, but not 2, then keep only the id:



          db.collection.aggregate([
          // group user id and all actions together

          $group:
          _id: "$user_id",
          actions:
          $addToSet: "$action"


          ,
          // filter documents which have 1 as action but not 2

          $match:
          $and: [

          "actions": 1
          ,

          "actions":
          $not:
          $eq: 2



          ]

          ,
          // only keep the id

          $group:
          _id: "$_id"


          ])


          This returns:



          [

          "_id": 8
          ,

          "_id": 9

          ]


          Here is a link to a playgorund: https://mongoplayground.net/p/So4HjEXx3sn



          You should consider how you structure your documents. Your design looks a bit like a relational database. It is advisable to model your documents according to your read accesses (if possible). In this case you could an user_id and an actions field, which has already all action ids grouped together.






          share|improve this answer























          • thanks for your good answer.

            – mhndev
            Nov 16 '18 at 10:17













          3












          3








          3







          You can use the aggregation pipeline to first group all actions into one array plus the associated user id into objects and the filter to have action 1, but not 2, then keep only the id:



          db.collection.aggregate([
          // group user id and all actions together

          $group:
          _id: "$user_id",
          actions:
          $addToSet: "$action"


          ,
          // filter documents which have 1 as action but not 2

          $match:
          $and: [

          "actions": 1
          ,

          "actions":
          $not:
          $eq: 2



          ]

          ,
          // only keep the id

          $group:
          _id: "$_id"


          ])


          This returns:



          [

          "_id": 8
          ,

          "_id": 9

          ]


          Here is a link to a playgorund: https://mongoplayground.net/p/So4HjEXx3sn



          You should consider how you structure your documents. Your design looks a bit like a relational database. It is advisable to model your documents according to your read accesses (if possible). In this case you could an user_id and an actions field, which has already all action ids grouped together.






          share|improve this answer













          You can use the aggregation pipeline to first group all actions into one array plus the associated user id into objects and the filter to have action 1, but not 2, then keep only the id:



          db.collection.aggregate([
          // group user id and all actions together

          $group:
          _id: "$user_id",
          actions:
          $addToSet: "$action"


          ,
          // filter documents which have 1 as action but not 2

          $match:
          $and: [

          "actions": 1
          ,

          "actions":
          $not:
          $eq: 2



          ]

          ,
          // only keep the id

          $group:
          _id: "$_id"


          ])


          This returns:



          [

          "_id": 8
          ,

          "_id": 9

          ]


          Here is a link to a playgorund: https://mongoplayground.net/p/So4HjEXx3sn



          You should consider how you structure your documents. Your design looks a bit like a relational database. It is advisable to model your documents according to your read accesses (if possible). In this case you could an user_id and an actions field, which has already all action ids grouped together.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 9:55









          mbuechmannmbuechmann

          3,07121429




          3,07121429












          • thanks for your good answer.

            – mhndev
            Nov 16 '18 at 10:17

















          • thanks for your good answer.

            – mhndev
            Nov 16 '18 at 10:17
















          thanks for your good answer.

          – mhndev
          Nov 16 '18 at 10:17





          thanks for your good answer.

          – mhndev
          Nov 16 '18 at 10:17













          2














          You can use $group and $push the actions for the distinct user_id. Finally use $match with actions $eq ual to 1 but not equal ($ne) to 2



          db.collection.aggregate([
          "$group":
          "_id": "$user_id",
          "actions": "$push": "$action"
          ,
          "$match": "actions": "$eq": 1, "$ne": 2 ,
          { "$project": "_id": 1
          ])





          share|improve this answer

























          • thanks, I thinks both answers are correct, so I chose the first answer.

            – mhndev
            Nov 16 '18 at 10:18






          • 1





            yes But you don't need to use two $group stages here. Simply use $project because we already find the distinct user_id in the first $group stage.

            – Anthony Winzlet
            Nov 16 '18 at 10:28











          • yes that's right

            – mhndev
            Nov 16 '18 at 10:45






          • 1





            I have updated the answer. Even that it has less operators. You don't need to use $group, $and, $not aggregation here.

            – Anthony Winzlet
            Nov 16 '18 at 17:10












          • yes, I noticed, thanks

            – mhndev
            Nov 16 '18 at 17:13















          2














          You can use $group and $push the actions for the distinct user_id. Finally use $match with actions $eq ual to 1 but not equal ($ne) to 2



          db.collection.aggregate([
          "$group":
          "_id": "$user_id",
          "actions": "$push": "$action"
          ,
          "$match": "actions": "$eq": 1, "$ne": 2 ,
          { "$project": "_id": 1
          ])





          share|improve this answer

























          • thanks, I thinks both answers are correct, so I chose the first answer.

            – mhndev
            Nov 16 '18 at 10:18






          • 1





            yes But you don't need to use two $group stages here. Simply use $project because we already find the distinct user_id in the first $group stage.

            – Anthony Winzlet
            Nov 16 '18 at 10:28











          • yes that's right

            – mhndev
            Nov 16 '18 at 10:45






          • 1





            I have updated the answer. Even that it has less operators. You don't need to use $group, $and, $not aggregation here.

            – Anthony Winzlet
            Nov 16 '18 at 17:10












          • yes, I noticed, thanks

            – mhndev
            Nov 16 '18 at 17:13













          2












          2








          2







          You can use $group and $push the actions for the distinct user_id. Finally use $match with actions $eq ual to 1 but not equal ($ne) to 2



          db.collection.aggregate([
          "$group":
          "_id": "$user_id",
          "actions": "$push": "$action"
          ,
          "$match": "actions": "$eq": 1, "$ne": 2 ,
          { "$project": "_id": 1
          ])





          share|improve this answer















          You can use $group and $push the actions for the distinct user_id. Finally use $match with actions $eq ual to 1 but not equal ($ne) to 2



          db.collection.aggregate([
          "$group":
          "_id": "$user_id",
          "actions": "$push": "$action"
          ,
          "$match": "actions": "$eq": 1, "$ne": 2 ,
          { "$project": "_id": 1
          ])






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 16 '18 at 17:07

























          answered Nov 16 '18 at 9:57









          Anthony WinzletAnthony Winzlet

          18.2k42345




          18.2k42345












          • thanks, I thinks both answers are correct, so I chose the first answer.

            – mhndev
            Nov 16 '18 at 10:18






          • 1





            yes But you don't need to use two $group stages here. Simply use $project because we already find the distinct user_id in the first $group stage.

            – Anthony Winzlet
            Nov 16 '18 at 10:28











          • yes that's right

            – mhndev
            Nov 16 '18 at 10:45






          • 1





            I have updated the answer. Even that it has less operators. You don't need to use $group, $and, $not aggregation here.

            – Anthony Winzlet
            Nov 16 '18 at 17:10












          • yes, I noticed, thanks

            – mhndev
            Nov 16 '18 at 17:13

















          • thanks, I thinks both answers are correct, so I chose the first answer.

            – mhndev
            Nov 16 '18 at 10:18






          • 1





            yes But you don't need to use two $group stages here. Simply use $project because we already find the distinct user_id in the first $group stage.

            – Anthony Winzlet
            Nov 16 '18 at 10:28











          • yes that's right

            – mhndev
            Nov 16 '18 at 10:45






          • 1





            I have updated the answer. Even that it has less operators. You don't need to use $group, $and, $not aggregation here.

            – Anthony Winzlet
            Nov 16 '18 at 17:10












          • yes, I noticed, thanks

            – mhndev
            Nov 16 '18 at 17:13
















          thanks, I thinks both answers are correct, so I chose the first answer.

          – mhndev
          Nov 16 '18 at 10:18





          thanks, I thinks both answers are correct, so I chose the first answer.

          – mhndev
          Nov 16 '18 at 10:18




          1




          1





          yes But you don't need to use two $group stages here. Simply use $project because we already find the distinct user_id in the first $group stage.

          – Anthony Winzlet
          Nov 16 '18 at 10:28





          yes But you don't need to use two $group stages here. Simply use $project because we already find the distinct user_id in the first $group stage.

          – Anthony Winzlet
          Nov 16 '18 at 10:28













          yes that's right

          – mhndev
          Nov 16 '18 at 10:45





          yes that's right

          – mhndev
          Nov 16 '18 at 10:45




          1




          1





          I have updated the answer. Even that it has less operators. You don't need to use $group, $and, $not aggregation here.

          – Anthony Winzlet
          Nov 16 '18 at 17:10






          I have updated the answer. Even that it has less operators. You don't need to use $group, $and, $not aggregation here.

          – Anthony Winzlet
          Nov 16 '18 at 17:10














          yes, I noticed, thanks

          – mhndev
          Nov 16 '18 at 17:13





          yes, I noticed, thanks

          – mhndev
          Nov 16 '18 at 17:13

















          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%2f53334567%2fmongodb-where-not-exists-subtract-two-query-results%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

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          ReactJS Fetched API data displays live - need Data displayed static

          政党