How to update document with nested array in mongodb [duplicate]












0















This question already has an answer here:




  • Mongodb $push in nested array

    2 answers




I've got a document in a collection 'events' with the following structure:



{ 
"_id" : "abc",
"text" : "some text",
}


I wish to update the document by inserting an array named guestOwner. The result I want would update the document like this:



{ 
"_id" : "abc",
"text" : "some text",
"guestOwner" : [
{
"name" : "JACK BLACK",
"guests" : [
"GUEST1",
"GUEST2"
]
}
]
}


So I tried an mongo update with the following:



db.events.update({ _id: eventid }, { $push: { guestOwner: { name: username, guests: allGuests } } });


where 'username' is a string, and 'allGuests' is an array of names ["Guest1", "Guest2"]



The issue for me is when a subsequent update to the document occurs, I would want to push a the new 'allGuests' array into the existing one if the name is the same. For example, if a second update occurs with 'allGuests' = ["GUEST3"], and with the same name = "JACK BLACK", I would want the document to be:



{ 
"_id" : "abc",
"text" : "some text",
"guestOwner" : [
{
"name" : "JACK BLACK",
"guests" : [
"GUEST1",
"GUEST2"
"GUEST3"
]
}
]
}


BUT, if the document were updated with a different name = 'JOHN SMITH' where allGuests array = ["GUEST3"], it would create:



{ 
"_id" : "abc",
"text" : "some text",
"guestOwner" : [
{
"name" : "JACK BLACK",
"guests" : [
"GUEST1",
"GUEST2"
]
},
{
"name" : "JOHN SMITH",
"guests" : [
"GUEST3"
]
}
]
}


Would I need conditional statements surrounding the mongo update to check for guestOwner[0].name? Not sure if mongo could do this on its own, or if a bunch of logic is going to be necessary.










share|improve this question













marked as duplicate by Neil Lunn mongodb
Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 18 '18 at 21:48


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.




















    0















    This question already has an answer here:




    • Mongodb $push in nested array

      2 answers




    I've got a document in a collection 'events' with the following structure:



    { 
    "_id" : "abc",
    "text" : "some text",
    }


    I wish to update the document by inserting an array named guestOwner. The result I want would update the document like this:



    { 
    "_id" : "abc",
    "text" : "some text",
    "guestOwner" : [
    {
    "name" : "JACK BLACK",
    "guests" : [
    "GUEST1",
    "GUEST2"
    ]
    }
    ]
    }


    So I tried an mongo update with the following:



    db.events.update({ _id: eventid }, { $push: { guestOwner: { name: username, guests: allGuests } } });


    where 'username' is a string, and 'allGuests' is an array of names ["Guest1", "Guest2"]



    The issue for me is when a subsequent update to the document occurs, I would want to push a the new 'allGuests' array into the existing one if the name is the same. For example, if a second update occurs with 'allGuests' = ["GUEST3"], and with the same name = "JACK BLACK", I would want the document to be:



    { 
    "_id" : "abc",
    "text" : "some text",
    "guestOwner" : [
    {
    "name" : "JACK BLACK",
    "guests" : [
    "GUEST1",
    "GUEST2"
    "GUEST3"
    ]
    }
    ]
    }


    BUT, if the document were updated with a different name = 'JOHN SMITH' where allGuests array = ["GUEST3"], it would create:



    { 
    "_id" : "abc",
    "text" : "some text",
    "guestOwner" : [
    {
    "name" : "JACK BLACK",
    "guests" : [
    "GUEST1",
    "GUEST2"
    ]
    },
    {
    "name" : "JOHN SMITH",
    "guests" : [
    "GUEST3"
    ]
    }
    ]
    }


    Would I need conditional statements surrounding the mongo update to check for guestOwner[0].name? Not sure if mongo could do this on its own, or if a bunch of logic is going to be necessary.










    share|improve this question













    marked as duplicate by Neil Lunn mongodb
    Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 18 '18 at 21:48


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


















      0












      0








      0








      This question already has an answer here:




      • Mongodb $push in nested array

        2 answers




      I've got a document in a collection 'events' with the following structure:



      { 
      "_id" : "abc",
      "text" : "some text",
      }


      I wish to update the document by inserting an array named guestOwner. The result I want would update the document like this:



      { 
      "_id" : "abc",
      "text" : "some text",
      "guestOwner" : [
      {
      "name" : "JACK BLACK",
      "guests" : [
      "GUEST1",
      "GUEST2"
      ]
      }
      ]
      }


      So I tried an mongo update with the following:



      db.events.update({ _id: eventid }, { $push: { guestOwner: { name: username, guests: allGuests } } });


      where 'username' is a string, and 'allGuests' is an array of names ["Guest1", "Guest2"]



      The issue for me is when a subsequent update to the document occurs, I would want to push a the new 'allGuests' array into the existing one if the name is the same. For example, if a second update occurs with 'allGuests' = ["GUEST3"], and with the same name = "JACK BLACK", I would want the document to be:



      { 
      "_id" : "abc",
      "text" : "some text",
      "guestOwner" : [
      {
      "name" : "JACK BLACK",
      "guests" : [
      "GUEST1",
      "GUEST2"
      "GUEST3"
      ]
      }
      ]
      }


      BUT, if the document were updated with a different name = 'JOHN SMITH' where allGuests array = ["GUEST3"], it would create:



      { 
      "_id" : "abc",
      "text" : "some text",
      "guestOwner" : [
      {
      "name" : "JACK BLACK",
      "guests" : [
      "GUEST1",
      "GUEST2"
      ]
      },
      {
      "name" : "JOHN SMITH",
      "guests" : [
      "GUEST3"
      ]
      }
      ]
      }


      Would I need conditional statements surrounding the mongo update to check for guestOwner[0].name? Not sure if mongo could do this on its own, or if a bunch of logic is going to be necessary.










      share|improve this question














      This question already has an answer here:




      • Mongodb $push in nested array

        2 answers




      I've got a document in a collection 'events' with the following structure:



      { 
      "_id" : "abc",
      "text" : "some text",
      }


      I wish to update the document by inserting an array named guestOwner. The result I want would update the document like this:



      { 
      "_id" : "abc",
      "text" : "some text",
      "guestOwner" : [
      {
      "name" : "JACK BLACK",
      "guests" : [
      "GUEST1",
      "GUEST2"
      ]
      }
      ]
      }


      So I tried an mongo update with the following:



      db.events.update({ _id: eventid }, { $push: { guestOwner: { name: username, guests: allGuests } } });


      where 'username' is a string, and 'allGuests' is an array of names ["Guest1", "Guest2"]



      The issue for me is when a subsequent update to the document occurs, I would want to push a the new 'allGuests' array into the existing one if the name is the same. For example, if a second update occurs with 'allGuests' = ["GUEST3"], and with the same name = "JACK BLACK", I would want the document to be:



      { 
      "_id" : "abc",
      "text" : "some text",
      "guestOwner" : [
      {
      "name" : "JACK BLACK",
      "guests" : [
      "GUEST1",
      "GUEST2"
      "GUEST3"
      ]
      }
      ]
      }


      BUT, if the document were updated with a different name = 'JOHN SMITH' where allGuests array = ["GUEST3"], it would create:



      { 
      "_id" : "abc",
      "text" : "some text",
      "guestOwner" : [
      {
      "name" : "JACK BLACK",
      "guests" : [
      "GUEST1",
      "GUEST2"
      ]
      },
      {
      "name" : "JOHN SMITH",
      "guests" : [
      "GUEST3"
      ]
      }
      ]
      }


      Would I need conditional statements surrounding the mongo update to check for guestOwner[0].name? Not sure if mongo could do this on its own, or if a bunch of logic is going to be necessary.





      This question already has an answer here:




      • Mongodb $push in nested array

        2 answers








      mongodb






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 18 '18 at 1:43









      flimflam57

      6411621




      6411621




      marked as duplicate by Neil Lunn mongodb
      Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 18 '18 at 21:48


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by Neil Lunn mongodb
      Users with the  mongodb badge can single-handedly close mongodb questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 18 '18 at 21:48


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          1














          You could simply do an update where in the query section you would specify the name:



          db.events.update({
          "_id" : ObjectId("someId"), "guestOwner.name": "JACK BLACK"}, {
          $push: { "guestOwner.$.guests": "GUEST11" // etc }
          })


          If this returns the number of updated elements to be 1 you are good to go since the name exists etc.



          If it returns 0 then that name does not exists so you can run:



          db.events.update({"_id" : ObjectId("someId")}, {
          $addToSet: { guestOwner: { name: "JACK BLACK" // etc }}
          })


          It would save you one call since if you have to check if the record exists you would always do 2 rounds. One to check and another to take action based on the result. Here if the records is already there you only do one update.






          share|improve this answer





















          • Ah, yes. Makes perfect sense.
            – flimflam57
            Nov 18 '18 at 13:59




















          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You could simply do an update where in the query section you would specify the name:



          db.events.update({
          "_id" : ObjectId("someId"), "guestOwner.name": "JACK BLACK"}, {
          $push: { "guestOwner.$.guests": "GUEST11" // etc }
          })


          If this returns the number of updated elements to be 1 you are good to go since the name exists etc.



          If it returns 0 then that name does not exists so you can run:



          db.events.update({"_id" : ObjectId("someId")}, {
          $addToSet: { guestOwner: { name: "JACK BLACK" // etc }}
          })


          It would save you one call since if you have to check if the record exists you would always do 2 rounds. One to check and another to take action based on the result. Here if the records is already there you only do one update.






          share|improve this answer





















          • Ah, yes. Makes perfect sense.
            – flimflam57
            Nov 18 '18 at 13:59


















          1














          You could simply do an update where in the query section you would specify the name:



          db.events.update({
          "_id" : ObjectId("someId"), "guestOwner.name": "JACK BLACK"}, {
          $push: { "guestOwner.$.guests": "GUEST11" // etc }
          })


          If this returns the number of updated elements to be 1 you are good to go since the name exists etc.



          If it returns 0 then that name does not exists so you can run:



          db.events.update({"_id" : ObjectId("someId")}, {
          $addToSet: { guestOwner: { name: "JACK BLACK" // etc }}
          })


          It would save you one call since if you have to check if the record exists you would always do 2 rounds. One to check and another to take action based on the result. Here if the records is already there you only do one update.






          share|improve this answer





















          • Ah, yes. Makes perfect sense.
            – flimflam57
            Nov 18 '18 at 13:59
















          1












          1








          1






          You could simply do an update where in the query section you would specify the name:



          db.events.update({
          "_id" : ObjectId("someId"), "guestOwner.name": "JACK BLACK"}, {
          $push: { "guestOwner.$.guests": "GUEST11" // etc }
          })


          If this returns the number of updated elements to be 1 you are good to go since the name exists etc.



          If it returns 0 then that name does not exists so you can run:



          db.events.update({"_id" : ObjectId("someId")}, {
          $addToSet: { guestOwner: { name: "JACK BLACK" // etc }}
          })


          It would save you one call since if you have to check if the record exists you would always do 2 rounds. One to check and another to take action based on the result. Here if the records is already there you only do one update.






          share|improve this answer












          You could simply do an update where in the query section you would specify the name:



          db.events.update({
          "_id" : ObjectId("someId"), "guestOwner.name": "JACK BLACK"}, {
          $push: { "guestOwner.$.guests": "GUEST11" // etc }
          })


          If this returns the number of updated elements to be 1 you are good to go since the name exists etc.



          If it returns 0 then that name does not exists so you can run:



          db.events.update({"_id" : ObjectId("someId")}, {
          $addToSet: { guestOwner: { name: "JACK BLACK" // etc }}
          })


          It would save you one call since if you have to check if the record exists you would always do 2 rounds. One to check and another to take action based on the result. Here if the records is already there you only do one update.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 18 '18 at 7:33









          Akrion

          9,40711224




          9,40711224












          • Ah, yes. Makes perfect sense.
            – flimflam57
            Nov 18 '18 at 13:59




















          • Ah, yes. Makes perfect sense.
            – flimflam57
            Nov 18 '18 at 13:59


















          Ah, yes. Makes perfect sense.
          – flimflam57
          Nov 18 '18 at 13:59






          Ah, yes. Makes perfect sense.
          – flimflam57
          Nov 18 '18 at 13:59





          Popular posts from this blog

          mysqli_query(): Empty query in /home/lucindabrummitt/public_html/blog/wp-includes/wp-db.php on line 1924

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?