MongoDB query for two input arrays at same index?












1















I have two arrays A and B of length n defined by the input,



fruit_ids = [{id: "id1"}, {id: "id2"}, {id:"id3"}];
fruit_names = [{name: "Orange"},{name: "Kiwi"},{name: "Banana"}]



and MongoDB documents



{ farm_id: "3344", fruits: [{name: "Orange", id:"id1"}, {name: "Kiwi", id:"id67"}]}


Now I want to write a Mongo query such that it pulls items from particular farm_id specified at array fruit_ids and fruit_names but at same index,



for example for the above input, I want for farm_id: 3344 {name: "Orange", id:"id1"} to get deleted.



Can anyone please help me.










share|improve this question





























    1















    I have two arrays A and B of length n defined by the input,



    fruit_ids = [{id: "id1"}, {id: "id2"}, {id:"id3"}];
    fruit_names = [{name: "Orange"},{name: "Kiwi"},{name: "Banana"}]



    and MongoDB documents



    { farm_id: "3344", fruits: [{name: "Orange", id:"id1"}, {name: "Kiwi", id:"id67"}]}


    Now I want to write a Mongo query such that it pulls items from particular farm_id specified at array fruit_ids and fruit_names but at same index,



    for example for the above input, I want for farm_id: 3344 {name: "Orange", id:"id1"} to get deleted.



    Can anyone please help me.










    share|improve this question



























      1












      1








      1


      1






      I have two arrays A and B of length n defined by the input,



      fruit_ids = [{id: "id1"}, {id: "id2"}, {id:"id3"}];
      fruit_names = [{name: "Orange"},{name: "Kiwi"},{name: "Banana"}]



      and MongoDB documents



      { farm_id: "3344", fruits: [{name: "Orange", id:"id1"}, {name: "Kiwi", id:"id67"}]}


      Now I want to write a Mongo query such that it pulls items from particular farm_id specified at array fruit_ids and fruit_names but at same index,



      for example for the above input, I want for farm_id: 3344 {name: "Orange", id:"id1"} to get deleted.



      Can anyone please help me.










      share|improve this question
















      I have two arrays A and B of length n defined by the input,



      fruit_ids = [{id: "id1"}, {id: "id2"}, {id:"id3"}];
      fruit_names = [{name: "Orange"},{name: "Kiwi"},{name: "Banana"}]



      and MongoDB documents



      { farm_id: "3344", fruits: [{name: "Orange", id:"id1"}, {name: "Kiwi", id:"id67"}]}


      Now I want to write a Mongo query such that it pulls items from particular farm_id specified at array fruit_ids and fruit_names but at same index,



      for example for the above input, I want for farm_id: 3344 {name: "Orange", id:"id1"} to get deleted.



      Can anyone please help me.







      mongodb






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 27 '18 at 22:32







      Sudhanshu Gaur

















      asked Nov 27 '18 at 21:53









      Sudhanshu GaurSudhanshu Gaur

      2,65322246




      2,65322246
























          2 Answers
          2






          active

          oldest

          votes


















          1














          You can use $pullAll operator to remove all the matching elements and build your update statement dynamically using below code:



          var fruit_ids = [{id: "id1"}, {id: "id2"}, {id:"id3"}]; 
          var fruit_names = [{name: "Orange"},{name: "Apple"},{name: "Banana"}];

          var pullAll = {
          $pullAll: { fruits: fruit_ids.map((id, index) => Object.assign(fruit_names[index], id)) }
          }

          db.col.update({ farm_id: 3344 }, pullAll)


          This will only try to update the farm_id: 3344.






          share|improve this answer


























          • and where is $pull in you query.

            – Sudhanshu Gaur
            Nov 27 '18 at 22:19











          • @SudhanshuGaur apologies for that, I saw query in your description but if you just want to update your collection then please check my updated answer

            – mickl
            Nov 27 '18 at 22:30











          • But I want to update only for farm_id: 3344, not mutiple documents only for one farm_id.

            – Sudhanshu Gaur
            Nov 27 '18 at 22:33











          • I will just add farm_id: 3344 instead of {} and will remove multi?

            – Sudhanshu Gaur
            Nov 27 '18 at 22:33








          • 1





            @SudhanshuGaur yes, that's how I just modified my answer

            – mickl
            Nov 27 '18 at 22:34



















          0














          I was trying $pullAll as suggested by @mickl in his answer, but the thing is I had other fields inside my embedded documents and because $pullAll works only for exact matches, that's why I currently I am using $pull with $or on the array of embedded docs. I found this solution from this answer how-to-force-mongodb-pullall-to-disregard-document-order.



          let arr = [{name: "Orange", id:"id1"}, {name: "Kiwi", id:"id67"}];

          db.col.update(
          { farm_id: 3344 },
          { "$pull": { "fruits": { "$or": arr } }}
          )





          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%2f53508774%2fmongodb-query-for-two-input-arrays-at-same-index%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









            1














            You can use $pullAll operator to remove all the matching elements and build your update statement dynamically using below code:



            var fruit_ids = [{id: "id1"}, {id: "id2"}, {id:"id3"}]; 
            var fruit_names = [{name: "Orange"},{name: "Apple"},{name: "Banana"}];

            var pullAll = {
            $pullAll: { fruits: fruit_ids.map((id, index) => Object.assign(fruit_names[index], id)) }
            }

            db.col.update({ farm_id: 3344 }, pullAll)


            This will only try to update the farm_id: 3344.






            share|improve this answer


























            • and where is $pull in you query.

              – Sudhanshu Gaur
              Nov 27 '18 at 22:19











            • @SudhanshuGaur apologies for that, I saw query in your description but if you just want to update your collection then please check my updated answer

              – mickl
              Nov 27 '18 at 22:30











            • But I want to update only for farm_id: 3344, not mutiple documents only for one farm_id.

              – Sudhanshu Gaur
              Nov 27 '18 at 22:33











            • I will just add farm_id: 3344 instead of {} and will remove multi?

              – Sudhanshu Gaur
              Nov 27 '18 at 22:33








            • 1





              @SudhanshuGaur yes, that's how I just modified my answer

              – mickl
              Nov 27 '18 at 22:34
















            1














            You can use $pullAll operator to remove all the matching elements and build your update statement dynamically using below code:



            var fruit_ids = [{id: "id1"}, {id: "id2"}, {id:"id3"}]; 
            var fruit_names = [{name: "Orange"},{name: "Apple"},{name: "Banana"}];

            var pullAll = {
            $pullAll: { fruits: fruit_ids.map((id, index) => Object.assign(fruit_names[index], id)) }
            }

            db.col.update({ farm_id: 3344 }, pullAll)


            This will only try to update the farm_id: 3344.






            share|improve this answer


























            • and where is $pull in you query.

              – Sudhanshu Gaur
              Nov 27 '18 at 22:19











            • @SudhanshuGaur apologies for that, I saw query in your description but if you just want to update your collection then please check my updated answer

              – mickl
              Nov 27 '18 at 22:30











            • But I want to update only for farm_id: 3344, not mutiple documents only for one farm_id.

              – Sudhanshu Gaur
              Nov 27 '18 at 22:33











            • I will just add farm_id: 3344 instead of {} and will remove multi?

              – Sudhanshu Gaur
              Nov 27 '18 at 22:33








            • 1





              @SudhanshuGaur yes, that's how I just modified my answer

              – mickl
              Nov 27 '18 at 22:34














            1












            1








            1







            You can use $pullAll operator to remove all the matching elements and build your update statement dynamically using below code:



            var fruit_ids = [{id: "id1"}, {id: "id2"}, {id:"id3"}]; 
            var fruit_names = [{name: "Orange"},{name: "Apple"},{name: "Banana"}];

            var pullAll = {
            $pullAll: { fruits: fruit_ids.map((id, index) => Object.assign(fruit_names[index], id)) }
            }

            db.col.update({ farm_id: 3344 }, pullAll)


            This will only try to update the farm_id: 3344.






            share|improve this answer















            You can use $pullAll operator to remove all the matching elements and build your update statement dynamically using below code:



            var fruit_ids = [{id: "id1"}, {id: "id2"}, {id:"id3"}]; 
            var fruit_names = [{name: "Orange"},{name: "Apple"},{name: "Banana"}];

            var pullAll = {
            $pullAll: { fruits: fruit_ids.map((id, index) => Object.assign(fruit_names[index], id)) }
            }

            db.col.update({ farm_id: 3344 }, pullAll)


            This will only try to update the farm_id: 3344.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 27 '18 at 22:34

























            answered Nov 27 '18 at 22:01









            micklmickl

            14.4k51639




            14.4k51639













            • and where is $pull in you query.

              – Sudhanshu Gaur
              Nov 27 '18 at 22:19











            • @SudhanshuGaur apologies for that, I saw query in your description but if you just want to update your collection then please check my updated answer

              – mickl
              Nov 27 '18 at 22:30











            • But I want to update only for farm_id: 3344, not mutiple documents only for one farm_id.

              – Sudhanshu Gaur
              Nov 27 '18 at 22:33











            • I will just add farm_id: 3344 instead of {} and will remove multi?

              – Sudhanshu Gaur
              Nov 27 '18 at 22:33








            • 1





              @SudhanshuGaur yes, that's how I just modified my answer

              – mickl
              Nov 27 '18 at 22:34



















            • and where is $pull in you query.

              – Sudhanshu Gaur
              Nov 27 '18 at 22:19











            • @SudhanshuGaur apologies for that, I saw query in your description but if you just want to update your collection then please check my updated answer

              – mickl
              Nov 27 '18 at 22:30











            • But I want to update only for farm_id: 3344, not mutiple documents only for one farm_id.

              – Sudhanshu Gaur
              Nov 27 '18 at 22:33











            • I will just add farm_id: 3344 instead of {} and will remove multi?

              – Sudhanshu Gaur
              Nov 27 '18 at 22:33








            • 1





              @SudhanshuGaur yes, that's how I just modified my answer

              – mickl
              Nov 27 '18 at 22:34

















            and where is $pull in you query.

            – Sudhanshu Gaur
            Nov 27 '18 at 22:19





            and where is $pull in you query.

            – Sudhanshu Gaur
            Nov 27 '18 at 22:19













            @SudhanshuGaur apologies for that, I saw query in your description but if you just want to update your collection then please check my updated answer

            – mickl
            Nov 27 '18 at 22:30





            @SudhanshuGaur apologies for that, I saw query in your description but if you just want to update your collection then please check my updated answer

            – mickl
            Nov 27 '18 at 22:30













            But I want to update only for farm_id: 3344, not mutiple documents only for one farm_id.

            – Sudhanshu Gaur
            Nov 27 '18 at 22:33





            But I want to update only for farm_id: 3344, not mutiple documents only for one farm_id.

            – Sudhanshu Gaur
            Nov 27 '18 at 22:33













            I will just add farm_id: 3344 instead of {} and will remove multi?

            – Sudhanshu Gaur
            Nov 27 '18 at 22:33







            I will just add farm_id: 3344 instead of {} and will remove multi?

            – Sudhanshu Gaur
            Nov 27 '18 at 22:33






            1




            1





            @SudhanshuGaur yes, that's how I just modified my answer

            – mickl
            Nov 27 '18 at 22:34





            @SudhanshuGaur yes, that's how I just modified my answer

            – mickl
            Nov 27 '18 at 22:34













            0














            I was trying $pullAll as suggested by @mickl in his answer, but the thing is I had other fields inside my embedded documents and because $pullAll works only for exact matches, that's why I currently I am using $pull with $or on the array of embedded docs. I found this solution from this answer how-to-force-mongodb-pullall-to-disregard-document-order.



            let arr = [{name: "Orange", id:"id1"}, {name: "Kiwi", id:"id67"}];

            db.col.update(
            { farm_id: 3344 },
            { "$pull": { "fruits": { "$or": arr } }}
            )





            share|improve this answer




























              0














              I was trying $pullAll as suggested by @mickl in his answer, but the thing is I had other fields inside my embedded documents and because $pullAll works only for exact matches, that's why I currently I am using $pull with $or on the array of embedded docs. I found this solution from this answer how-to-force-mongodb-pullall-to-disregard-document-order.



              let arr = [{name: "Orange", id:"id1"}, {name: "Kiwi", id:"id67"}];

              db.col.update(
              { farm_id: 3344 },
              { "$pull": { "fruits": { "$or": arr } }}
              )





              share|improve this answer


























                0












                0








                0







                I was trying $pullAll as suggested by @mickl in his answer, but the thing is I had other fields inside my embedded documents and because $pullAll works only for exact matches, that's why I currently I am using $pull with $or on the array of embedded docs. I found this solution from this answer how-to-force-mongodb-pullall-to-disregard-document-order.



                let arr = [{name: "Orange", id:"id1"}, {name: "Kiwi", id:"id67"}];

                db.col.update(
                { farm_id: 3344 },
                { "$pull": { "fruits": { "$or": arr } }}
                )





                share|improve this answer













                I was trying $pullAll as suggested by @mickl in his answer, but the thing is I had other fields inside my embedded documents and because $pullAll works only for exact matches, that's why I currently I am using $pull with $or on the array of embedded docs. I found this solution from this answer how-to-force-mongodb-pullall-to-disregard-document-order.



                let arr = [{name: "Orange", id:"id1"}, {name: "Kiwi", id:"id67"}];

                db.col.update(
                { farm_id: 3344 },
                { "$pull": { "fruits": { "$or": arr } }}
                )






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 3 '18 at 14:28









                Sudhanshu GaurSudhanshu Gaur

                2,65322246




                2,65322246






























                    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%2f53508774%2fmongodb-query-for-two-input-arrays-at-same-index%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

                    Lallio

                    Unable to find Lightning Node

                    Futebolista