How to query an object in an array embedded in mongodb?












1















I am currently working on an application that takes control of Projects, which have Meetings and that these meetings have Participants.



I want to consult a Participant by his nomina field.



Structure for a project document object:



 {
"id":"5c1b0616a0441f27f022bfdc",
"name":"Project Test",
"area":"Area",
"date":"2019-01-01",
"meetings":[
{
"id":"5c1b073d445707834699ce97",
"objetive":"Objetive",
"fecha":"2019-01-01",
"participants":[
{
"nomina":1,
"name":"Person 1",
"role":"Rol1",
"area":"area1",
"signature":null
},
{
"nomina":2,
"name":"Person 2",
"role":"rol 2",
"area":"área 2",
"signature":null
}
]
}
]
}


Expected behavior



I want to consult a Participant by nomina field knowing the id of the Project and also knowing the id of the Meeting.



Expected output



Having:





  • id Project = 5c1b0616a0441f27f022bfdc


  • id Meeting = 5c1b073d445707834699ce97


  • nomina Participant = 1


It's expected that the query will return me:



{    
"nomina":1,
"name":"Person 1",
"role":"Rol1",
"area":"area1",
"signature":null
}









share|improve this question



























    1















    I am currently working on an application that takes control of Projects, which have Meetings and that these meetings have Participants.



    I want to consult a Participant by his nomina field.



    Structure for a project document object:



     {
    "id":"5c1b0616a0441f27f022bfdc",
    "name":"Project Test",
    "area":"Area",
    "date":"2019-01-01",
    "meetings":[
    {
    "id":"5c1b073d445707834699ce97",
    "objetive":"Objetive",
    "fecha":"2019-01-01",
    "participants":[
    {
    "nomina":1,
    "name":"Person 1",
    "role":"Rol1",
    "area":"area1",
    "signature":null
    },
    {
    "nomina":2,
    "name":"Person 2",
    "role":"rol 2",
    "area":"área 2",
    "signature":null
    }
    ]
    }
    ]
    }


    Expected behavior



    I want to consult a Participant by nomina field knowing the id of the Project and also knowing the id of the Meeting.



    Expected output



    Having:





    • id Project = 5c1b0616a0441f27f022bfdc


    • id Meeting = 5c1b073d445707834699ce97


    • nomina Participant = 1


    It's expected that the query will return me:



    {    
    "nomina":1,
    "name":"Person 1",
    "role":"Rol1",
    "area":"area1",
    "signature":null
    }









    share|improve this question

























      1












      1








      1








      I am currently working on an application that takes control of Projects, which have Meetings and that these meetings have Participants.



      I want to consult a Participant by his nomina field.



      Structure for a project document object:



       {
      "id":"5c1b0616a0441f27f022bfdc",
      "name":"Project Test",
      "area":"Area",
      "date":"2019-01-01",
      "meetings":[
      {
      "id":"5c1b073d445707834699ce97",
      "objetive":"Objetive",
      "fecha":"2019-01-01",
      "participants":[
      {
      "nomina":1,
      "name":"Person 1",
      "role":"Rol1",
      "area":"area1",
      "signature":null
      },
      {
      "nomina":2,
      "name":"Person 2",
      "role":"rol 2",
      "area":"área 2",
      "signature":null
      }
      ]
      }
      ]
      }


      Expected behavior



      I want to consult a Participant by nomina field knowing the id of the Project and also knowing the id of the Meeting.



      Expected output



      Having:





      • id Project = 5c1b0616a0441f27f022bfdc


      • id Meeting = 5c1b073d445707834699ce97


      • nomina Participant = 1


      It's expected that the query will return me:



      {    
      "nomina":1,
      "name":"Person 1",
      "role":"Rol1",
      "area":"area1",
      "signature":null
      }









      share|improve this question














      I am currently working on an application that takes control of Projects, which have Meetings and that these meetings have Participants.



      I want to consult a Participant by his nomina field.



      Structure for a project document object:



       {
      "id":"5c1b0616a0441f27f022bfdc",
      "name":"Project Test",
      "area":"Area",
      "date":"2019-01-01",
      "meetings":[
      {
      "id":"5c1b073d445707834699ce97",
      "objetive":"Objetive",
      "fecha":"2019-01-01",
      "participants":[
      {
      "nomina":1,
      "name":"Person 1",
      "role":"Rol1",
      "area":"area1",
      "signature":null
      },
      {
      "nomina":2,
      "name":"Person 2",
      "role":"rol 2",
      "area":"área 2",
      "signature":null
      }
      ]
      }
      ]
      }


      Expected behavior



      I want to consult a Participant by nomina field knowing the id of the Project and also knowing the id of the Meeting.



      Expected output



      Having:





      • id Project = 5c1b0616a0441f27f022bfdc


      • id Meeting = 5c1b073d445707834699ce97


      • nomina Participant = 1


      It's expected that the query will return me:



      {    
      "nomina":1,
      "name":"Person 1",
      "role":"Rol1",
      "area":"area1",
      "signature":null
      }






      mongodb






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 2 at 13:47









      GibránGibrán

      1418




      1418
























          1 Answer
          1






          active

          oldest

          votes


















          1














          For not so huge number of meetings in every document if you want to get the exact document stated, you can do this pipeline, it is straight forward:



          db.collection.aggregate(
          [
          {
          $match: {
          id:"5c1b0616a0441f27f022bfdc"
          }
          }, {
          $unwind: {
          path : "$meetings"
          }
          },
          {
          $unwind: {
          path : "$meetings.participants"
          }
          },
          {
          $match: {
          "meetings.id":"5c1b073d445707834699ce97",
          "meetings.participants.nomina":1

          }
          },
          {
          $replaceRoot: {
          newRoot: "$meetings.participants"
          }
          }
          ]);


          If you would have over thousands of elements in meetings then I'd suggest adding another match to meetings or grouping meetings and project IDs.
          But if you just want to get the document containing what you want it is just a simple find query:



          db.collection.find({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 });





          share|improve this answer


























          • Hi, thanks. db.collection.find({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 }); this will always return me the entire document? I ran the example here

            – Gibrán
            Jan 2 at 18:48








          • 1





            Yes, that would bring the whole document containing the conditions you want, but if you have at most a couple of thousand elements in "meetings" array, you can easily go with aggregation pipeline.

            – Babak
            Jan 2 at 19:13











          • Sorry, and now that I know the Participant with nomina= 1, how could modify his signature field?

            – Gibrán
            Jan 2 at 21:07











          • You mean you want to update that document ? nodechef.com/docs/cloud-search/… Something like this should work: db.collection.update({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 },{$set:{"meetings.participants.$$.signature":1122}}, false, true )

            – Babak
            Jan 2 at 21:51








          • 1





            Oh yup, sorry about that, what is the tool or language that your are working with mongo on ?

            – Babak
            Jan 3 at 15:07











          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%2f54007497%2fhow-to-query-an-object-in-an-array-embedded-in-mongodb%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









          1














          For not so huge number of meetings in every document if you want to get the exact document stated, you can do this pipeline, it is straight forward:



          db.collection.aggregate(
          [
          {
          $match: {
          id:"5c1b0616a0441f27f022bfdc"
          }
          }, {
          $unwind: {
          path : "$meetings"
          }
          },
          {
          $unwind: {
          path : "$meetings.participants"
          }
          },
          {
          $match: {
          "meetings.id":"5c1b073d445707834699ce97",
          "meetings.participants.nomina":1

          }
          },
          {
          $replaceRoot: {
          newRoot: "$meetings.participants"
          }
          }
          ]);


          If you would have over thousands of elements in meetings then I'd suggest adding another match to meetings or grouping meetings and project IDs.
          But if you just want to get the document containing what you want it is just a simple find query:



          db.collection.find({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 });





          share|improve this answer


























          • Hi, thanks. db.collection.find({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 }); this will always return me the entire document? I ran the example here

            – Gibrán
            Jan 2 at 18:48








          • 1





            Yes, that would bring the whole document containing the conditions you want, but if you have at most a couple of thousand elements in "meetings" array, you can easily go with aggregation pipeline.

            – Babak
            Jan 2 at 19:13











          • Sorry, and now that I know the Participant with nomina= 1, how could modify his signature field?

            – Gibrán
            Jan 2 at 21:07











          • You mean you want to update that document ? nodechef.com/docs/cloud-search/… Something like this should work: db.collection.update({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 },{$set:{"meetings.participants.$$.signature":1122}}, false, true )

            – Babak
            Jan 2 at 21:51








          • 1





            Oh yup, sorry about that, what is the tool or language that your are working with mongo on ?

            – Babak
            Jan 3 at 15:07
















          1














          For not so huge number of meetings in every document if you want to get the exact document stated, you can do this pipeline, it is straight forward:



          db.collection.aggregate(
          [
          {
          $match: {
          id:"5c1b0616a0441f27f022bfdc"
          }
          }, {
          $unwind: {
          path : "$meetings"
          }
          },
          {
          $unwind: {
          path : "$meetings.participants"
          }
          },
          {
          $match: {
          "meetings.id":"5c1b073d445707834699ce97",
          "meetings.participants.nomina":1

          }
          },
          {
          $replaceRoot: {
          newRoot: "$meetings.participants"
          }
          }
          ]);


          If you would have over thousands of elements in meetings then I'd suggest adding another match to meetings or grouping meetings and project IDs.
          But if you just want to get the document containing what you want it is just a simple find query:



          db.collection.find({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 });





          share|improve this answer


























          • Hi, thanks. db.collection.find({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 }); this will always return me the entire document? I ran the example here

            – Gibrán
            Jan 2 at 18:48








          • 1





            Yes, that would bring the whole document containing the conditions you want, but if you have at most a couple of thousand elements in "meetings" array, you can easily go with aggregation pipeline.

            – Babak
            Jan 2 at 19:13











          • Sorry, and now that I know the Participant with nomina= 1, how could modify his signature field?

            – Gibrán
            Jan 2 at 21:07











          • You mean you want to update that document ? nodechef.com/docs/cloud-search/… Something like this should work: db.collection.update({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 },{$set:{"meetings.participants.$$.signature":1122}}, false, true )

            – Babak
            Jan 2 at 21:51








          • 1





            Oh yup, sorry about that, what is the tool or language that your are working with mongo on ?

            – Babak
            Jan 3 at 15:07














          1












          1








          1







          For not so huge number of meetings in every document if you want to get the exact document stated, you can do this pipeline, it is straight forward:



          db.collection.aggregate(
          [
          {
          $match: {
          id:"5c1b0616a0441f27f022bfdc"
          }
          }, {
          $unwind: {
          path : "$meetings"
          }
          },
          {
          $unwind: {
          path : "$meetings.participants"
          }
          },
          {
          $match: {
          "meetings.id":"5c1b073d445707834699ce97",
          "meetings.participants.nomina":1

          }
          },
          {
          $replaceRoot: {
          newRoot: "$meetings.participants"
          }
          }
          ]);


          If you would have over thousands of elements in meetings then I'd suggest adding another match to meetings or grouping meetings and project IDs.
          But if you just want to get the document containing what you want it is just a simple find query:



          db.collection.find({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 });





          share|improve this answer















          For not so huge number of meetings in every document if you want to get the exact document stated, you can do this pipeline, it is straight forward:



          db.collection.aggregate(
          [
          {
          $match: {
          id:"5c1b0616a0441f27f022bfdc"
          }
          }, {
          $unwind: {
          path : "$meetings"
          }
          },
          {
          $unwind: {
          path : "$meetings.participants"
          }
          },
          {
          $match: {
          "meetings.id":"5c1b073d445707834699ce97",
          "meetings.participants.nomina":1

          }
          },
          {
          $replaceRoot: {
          newRoot: "$meetings.participants"
          }
          }
          ]);


          If you would have over thousands of elements in meetings then I'd suggest adding another match to meetings or grouping meetings and project IDs.
          But if you just want to get the document containing what you want it is just a simple find query:



          db.collection.find({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 });






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 15:05

























          answered Jan 2 at 14:59









          BabakBabak

          486310




          486310













          • Hi, thanks. db.collection.find({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 }); this will always return me the entire document? I ran the example here

            – Gibrán
            Jan 2 at 18:48








          • 1





            Yes, that would bring the whole document containing the conditions you want, but if you have at most a couple of thousand elements in "meetings" array, you can easily go with aggregation pipeline.

            – Babak
            Jan 2 at 19:13











          • Sorry, and now that I know the Participant with nomina= 1, how could modify his signature field?

            – Gibrán
            Jan 2 at 21:07











          • You mean you want to update that document ? nodechef.com/docs/cloud-search/… Something like this should work: db.collection.update({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 },{$set:{"meetings.participants.$$.signature":1122}}, false, true )

            – Babak
            Jan 2 at 21:51








          • 1





            Oh yup, sorry about that, what is the tool or language that your are working with mongo on ?

            – Babak
            Jan 3 at 15:07



















          • Hi, thanks. db.collection.find({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 }); this will always return me the entire document? I ran the example here

            – Gibrán
            Jan 2 at 18:48








          • 1





            Yes, that would bring the whole document containing the conditions you want, but if you have at most a couple of thousand elements in "meetings" array, you can easily go with aggregation pipeline.

            – Babak
            Jan 2 at 19:13











          • Sorry, and now that I know the Participant with nomina= 1, how could modify his signature field?

            – Gibrán
            Jan 2 at 21:07











          • You mean you want to update that document ? nodechef.com/docs/cloud-search/… Something like this should work: db.collection.update({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 },{$set:{"meetings.participants.$$.signature":1122}}, false, true )

            – Babak
            Jan 2 at 21:51








          • 1





            Oh yup, sorry about that, what is the tool or language that your are working with mongo on ?

            – Babak
            Jan 3 at 15:07

















          Hi, thanks. db.collection.find({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 }); this will always return me the entire document? I ran the example here

          – Gibrán
          Jan 2 at 18:48







          Hi, thanks. db.collection.find({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 }); this will always return me the entire document? I ran the example here

          – Gibrán
          Jan 2 at 18:48






          1




          1





          Yes, that would bring the whole document containing the conditions you want, but if you have at most a couple of thousand elements in "meetings" array, you can easily go with aggregation pipeline.

          – Babak
          Jan 2 at 19:13





          Yes, that would bring the whole document containing the conditions you want, but if you have at most a couple of thousand elements in "meetings" array, you can easily go with aggregation pipeline.

          – Babak
          Jan 2 at 19:13













          Sorry, and now that I know the Participant with nomina= 1, how could modify his signature field?

          – Gibrán
          Jan 2 at 21:07





          Sorry, and now that I know the Participant with nomina= 1, how could modify his signature field?

          – Gibrán
          Jan 2 at 21:07













          You mean you want to update that document ? nodechef.com/docs/cloud-search/… Something like this should work: db.collection.update({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 },{$set:{"meetings.participants.$$.signature":1122}}, false, true )

          – Babak
          Jan 2 at 21:51







          You mean you want to update that document ? nodechef.com/docs/cloud-search/… Something like this should work: db.collection.update({id:"5c1b0616a0441f27f022bfdc","meetings.id":"5c1b073d445707834699ce97","meetings.participants.nomina":1 },{$set:{"meetings.participants.$$.signature":1122}}, false, true )

          – Babak
          Jan 2 at 21:51






          1




          1





          Oh yup, sorry about that, what is the tool or language that your are working with mongo on ?

          – Babak
          Jan 3 at 15:07





          Oh yup, sorry about that, what is the tool or language that your are working with mongo on ?

          – Babak
          Jan 3 at 15:07




















          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%2f54007497%2fhow-to-query-an-object-in-an-array-embedded-in-mongodb%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

          Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

          Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

          A Topological Invariant for $pi_3(U(n))$