Update Field in Array of Objects MongoDb [duplicate]












0
















This question already has an answer here:




  • How do I update Array Elements matching criteria in a MongoDB document?

    1 answer




I have an object that looks like the following:



> db.delegate_list.find({"name": "delegateList"}).pretty()
{
"_id" : ObjectId("5becd1e0adeb2717c087cec5"),
"name" : "delegateList",
"delegates" : [
{
"address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafsdkjhc",
"sync" : false
},
{
"address" : "tz1MecudVJnFZN5FSrriu8ULz2d6dDTR7KaM",
"sync" : false
}
]
}


I am trying to update the sync field for specific object in the array. So for example if I update the sync field for the object at index 0 or address tz1SUgyRB8T5jXgXAwS33pgRHAKrafsdkjhc by doing the following it yields:



> db.delegate_list.update({"name":"delegateList"}, {$set:{"delegates.0.sync": true}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.delegate_list.find({"name": "delegateList"}).pretty()
{
"_id" : ObjectId("5becd1e0adeb2717c087cec5"),
"name" : "delegateList",
"delegates" : [
{
"address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc",
"sync" : true
},
{
"address" : "tz1MecudVJnFZN5FSrriu8ULz2d6dDTR7KaM",
"sync" : false
}
]
}


It works as expected, but if I perform the same update command to switch back sync:false, it gives me a malformed object, while deleting the rest of the array:



> db.delegate_list.update({"name":"delegateList"}, {$set:{"delegates.0.sync": false}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.delegate_list.find({"name": "delegateList"}).pretty()
{
"_id" : ObjectId("5becd1e0adeb2717c087cec5"),
"name" : "delegateList",
"delegates" : {
"address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc",
"sync" : true,
"0" : {
"sync" : false
}
}
}









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 15 '18 at 5: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:




    • How do I update Array Elements matching criteria in a MongoDB document?

      1 answer




    I have an object that looks like the following:



    > db.delegate_list.find({"name": "delegateList"}).pretty()
    {
    "_id" : ObjectId("5becd1e0adeb2717c087cec5"),
    "name" : "delegateList",
    "delegates" : [
    {
    "address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafsdkjhc",
    "sync" : false
    },
    {
    "address" : "tz1MecudVJnFZN5FSrriu8ULz2d6dDTR7KaM",
    "sync" : false
    }
    ]
    }


    I am trying to update the sync field for specific object in the array. So for example if I update the sync field for the object at index 0 or address tz1SUgyRB8T5jXgXAwS33pgRHAKrafsdkjhc by doing the following it yields:



    > db.delegate_list.update({"name":"delegateList"}, {$set:{"delegates.0.sync": true}})
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    > db.delegate_list.find({"name": "delegateList"}).pretty()
    {
    "_id" : ObjectId("5becd1e0adeb2717c087cec5"),
    "name" : "delegateList",
    "delegates" : [
    {
    "address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc",
    "sync" : true
    },
    {
    "address" : "tz1MecudVJnFZN5FSrriu8ULz2d6dDTR7KaM",
    "sync" : false
    }
    ]
    }


    It works as expected, but if I perform the same update command to switch back sync:false, it gives me a malformed object, while deleting the rest of the array:



    > db.delegate_list.update({"name":"delegateList"}, {$set:{"delegates.0.sync": false}})
    WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
    > db.delegate_list.find({"name": "delegateList"}).pretty()
    {
    "_id" : ObjectId("5becd1e0adeb2717c087cec5"),
    "name" : "delegateList",
    "delegates" : {
    "address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc",
    "sync" : true,
    "0" : {
    "sync" : false
    }
    }
    }









    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 15 '18 at 5: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:




      • How do I update Array Elements matching criteria in a MongoDB document?

        1 answer




      I have an object that looks like the following:



      > db.delegate_list.find({"name": "delegateList"}).pretty()
      {
      "_id" : ObjectId("5becd1e0adeb2717c087cec5"),
      "name" : "delegateList",
      "delegates" : [
      {
      "address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafsdkjhc",
      "sync" : false
      },
      {
      "address" : "tz1MecudVJnFZN5FSrriu8ULz2d6dDTR7KaM",
      "sync" : false
      }
      ]
      }


      I am trying to update the sync field for specific object in the array. So for example if I update the sync field for the object at index 0 or address tz1SUgyRB8T5jXgXAwS33pgRHAKrafsdkjhc by doing the following it yields:



      > db.delegate_list.update({"name":"delegateList"}, {$set:{"delegates.0.sync": true}})
      WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
      > db.delegate_list.find({"name": "delegateList"}).pretty()
      {
      "_id" : ObjectId("5becd1e0adeb2717c087cec5"),
      "name" : "delegateList",
      "delegates" : [
      {
      "address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc",
      "sync" : true
      },
      {
      "address" : "tz1MecudVJnFZN5FSrriu8ULz2d6dDTR7KaM",
      "sync" : false
      }
      ]
      }


      It works as expected, but if I perform the same update command to switch back sync:false, it gives me a malformed object, while deleting the rest of the array:



      > db.delegate_list.update({"name":"delegateList"}, {$set:{"delegates.0.sync": false}})
      WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
      > db.delegate_list.find({"name": "delegateList"}).pretty()
      {
      "_id" : ObjectId("5becd1e0adeb2717c087cec5"),
      "name" : "delegateList",
      "delegates" : {
      "address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc",
      "sync" : true,
      "0" : {
      "sync" : false
      }
      }
      }









      share|improve this question















      This question already has an answer here:




      • How do I update Array Elements matching criteria in a MongoDB document?

        1 answer




      I have an object that looks like the following:



      > db.delegate_list.find({"name": "delegateList"}).pretty()
      {
      "_id" : ObjectId("5becd1e0adeb2717c087cec5"),
      "name" : "delegateList",
      "delegates" : [
      {
      "address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafsdkjhc",
      "sync" : false
      },
      {
      "address" : "tz1MecudVJnFZN5FSrriu8ULz2d6dDTR7KaM",
      "sync" : false
      }
      ]
      }


      I am trying to update the sync field for specific object in the array. So for example if I update the sync field for the object at index 0 or address tz1SUgyRB8T5jXgXAwS33pgRHAKrafsdkjhc by doing the following it yields:



      > db.delegate_list.update({"name":"delegateList"}, {$set:{"delegates.0.sync": true}})
      WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
      > db.delegate_list.find({"name": "delegateList"}).pretty()
      {
      "_id" : ObjectId("5becd1e0adeb2717c087cec5"),
      "name" : "delegateList",
      "delegates" : [
      {
      "address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc",
      "sync" : true
      },
      {
      "address" : "tz1MecudVJnFZN5FSrriu8ULz2d6dDTR7KaM",
      "sync" : false
      }
      ]
      }


      It works as expected, but if I perform the same update command to switch back sync:false, it gives me a malformed object, while deleting the rest of the array:



      > db.delegate_list.update({"name":"delegateList"}, {$set:{"delegates.0.sync": false}})
      WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
      > db.delegate_list.find({"name": "delegateList"}).pretty()
      {
      "_id" : ObjectId("5becd1e0adeb2717c087cec5"),
      "name" : "delegateList",
      "delegates" : {
      "address" : "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc",
      "sync" : true,
      "0" : {
      "sync" : false
      }
      }
      }




      This question already has an answer here:




      • How do I update Array Elements matching criteria in a MongoDB document?

        1 answer








      mongodb






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 2:25









      DefinitelyNotAGoatDefinitelyNotAGoat

      1011




      1011




      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 15 '18 at 5: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 15 '18 at 5: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


















          0














          You can update specific array item, if item match your case using $ operator



           db.delegate_list.update({"delegates.address":"tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc"}, {$set:{"delegates.$.sync": false}})

          It will update the sync filed which address is "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc"
          only, and not going to add any extra field.





          share|improve this answer






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You can update specific array item, if item match your case using $ operator



             db.delegate_list.update({"delegates.address":"tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc"}, {$set:{"delegates.$.sync": false}})

            It will update the sync filed which address is "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc"
            only, and not going to add any extra field.





            share|improve this answer




























              0














              You can update specific array item, if item match your case using $ operator



               db.delegate_list.update({"delegates.address":"tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc"}, {$set:{"delegates.$.sync": false}})

              It will update the sync filed which address is "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc"
              only, and not going to add any extra field.





              share|improve this answer


























                0












                0








                0







                You can update specific array item, if item match your case using $ operator



                 db.delegate_list.update({"delegates.address":"tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc"}, {$set:{"delegates.$.sync": false}})

                It will update the sync filed which address is "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc"
                only, and not going to add any extra field.





                share|improve this answer













                You can update specific array item, if item match your case using $ operator



                 db.delegate_list.update({"delegates.address":"tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc"}, {$set:{"delegates.$.sync": false}})

                It will update the sync filed which address is "tz1SUgyRB8T5jXgXAwS33pgRHAKrafyg87Yc"
                only, and not going to add any extra field.






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 15 '18 at 4:12









                Biplab MalakarBiplab Malakar

                42837




                42837

















                    Popular posts from this blog

                    Bressuire

                    Vorschmack

                    Quarantine