Updating a field in a nested object when multiple conditions are met using Mongoose











up vote
0
down vote

favorite












I have a data model with nested objects, and I'd like to update the 'available' field to 'reserved' using the main object ID, plus the date and time associated with the nested object. Here's my data...



{
"_id": {
"$oid": "5bf1a675041f242cda138d1c"
},
"availability": [
{
"_id": {
"$oid": "5bf2c077d5683c38afacf34b"
},
"date": "2018-12-20 ",
"time": " 11:00 - 12:00 ",
"status": "reserved"
},
{
"_id": {
"$oid": "5bf2c3bb8b95bd3a7cb3eabe"
},
"date": "2018-11-30",
"time": "18:00 - 19:00",
"status": "available"
}
],
"name": "blah",
"price": 50,
"city": "blah",
"__v": 0
}


And the code I'm trying...



router.get('/reserve/:id', function(req, res, next){

//I've other stuff here that sets the correct variables according to the console.log, so str2[0] contains the date I want to find, etc.


Pitch.findOneAndUpdate({
id: req.params.id,
'availability.date': 'str2[0]', //passing in 2018-11-30
'availability.time': 'str2[1]', //passing in 18:00 - 19:00
'availability.status': 'available'
},
{$set: {
'availability.$.status': 'reserved'
}
})


Yet, the object won't update and set the status to 'reserved'. Can anyone spot what I'm doing wrong? Should I try to access the particular object using different conditions?



EDIT : Through trial and error, I think the problem is that the 'find' part returns the parent object, and all of its sub-objects in the 'availability' array are returned. The the 'update' part doesn't know which 'status' to update, and is failing at this point.



I was returning the date and time from a form in the front end and thought that I could identify the object in the array from it's parent ID plus the date and time of the reservation.



So I suppose my question should be, as the id returned in the URL is the parent object ID, is there a way to uniquely extract a specified availability object (eg. 5bf2c3bb8b95bd3a7cb3eabe) and update it's status to 'reserved'?



EDIT 2 : I can also 'find' an item by it's 'availability._id' but the full parent object is returned, so the 'update status' part doesn't work as it's too broad.










share|improve this question




























    up vote
    0
    down vote

    favorite












    I have a data model with nested objects, and I'd like to update the 'available' field to 'reserved' using the main object ID, plus the date and time associated with the nested object. Here's my data...



    {
    "_id": {
    "$oid": "5bf1a675041f242cda138d1c"
    },
    "availability": [
    {
    "_id": {
    "$oid": "5bf2c077d5683c38afacf34b"
    },
    "date": "2018-12-20 ",
    "time": " 11:00 - 12:00 ",
    "status": "reserved"
    },
    {
    "_id": {
    "$oid": "5bf2c3bb8b95bd3a7cb3eabe"
    },
    "date": "2018-11-30",
    "time": "18:00 - 19:00",
    "status": "available"
    }
    ],
    "name": "blah",
    "price": 50,
    "city": "blah",
    "__v": 0
    }


    And the code I'm trying...



    router.get('/reserve/:id', function(req, res, next){

    //I've other stuff here that sets the correct variables according to the console.log, so str2[0] contains the date I want to find, etc.


    Pitch.findOneAndUpdate({
    id: req.params.id,
    'availability.date': 'str2[0]', //passing in 2018-11-30
    'availability.time': 'str2[1]', //passing in 18:00 - 19:00
    'availability.status': 'available'
    },
    {$set: {
    'availability.$.status': 'reserved'
    }
    })


    Yet, the object won't update and set the status to 'reserved'. Can anyone spot what I'm doing wrong? Should I try to access the particular object using different conditions?



    EDIT : Through trial and error, I think the problem is that the 'find' part returns the parent object, and all of its sub-objects in the 'availability' array are returned. The the 'update' part doesn't know which 'status' to update, and is failing at this point.



    I was returning the date and time from a form in the front end and thought that I could identify the object in the array from it's parent ID plus the date and time of the reservation.



    So I suppose my question should be, as the id returned in the URL is the parent object ID, is there a way to uniquely extract a specified availability object (eg. 5bf2c3bb8b95bd3a7cb3eabe) and update it's status to 'reserved'?



    EDIT 2 : I can also 'find' an item by it's 'availability._id' but the full parent object is returned, so the 'update status' part doesn't work as it's too broad.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a data model with nested objects, and I'd like to update the 'available' field to 'reserved' using the main object ID, plus the date and time associated with the nested object. Here's my data...



      {
      "_id": {
      "$oid": "5bf1a675041f242cda138d1c"
      },
      "availability": [
      {
      "_id": {
      "$oid": "5bf2c077d5683c38afacf34b"
      },
      "date": "2018-12-20 ",
      "time": " 11:00 - 12:00 ",
      "status": "reserved"
      },
      {
      "_id": {
      "$oid": "5bf2c3bb8b95bd3a7cb3eabe"
      },
      "date": "2018-11-30",
      "time": "18:00 - 19:00",
      "status": "available"
      }
      ],
      "name": "blah",
      "price": 50,
      "city": "blah",
      "__v": 0
      }


      And the code I'm trying...



      router.get('/reserve/:id', function(req, res, next){

      //I've other stuff here that sets the correct variables according to the console.log, so str2[0] contains the date I want to find, etc.


      Pitch.findOneAndUpdate({
      id: req.params.id,
      'availability.date': 'str2[0]', //passing in 2018-11-30
      'availability.time': 'str2[1]', //passing in 18:00 - 19:00
      'availability.status': 'available'
      },
      {$set: {
      'availability.$.status': 'reserved'
      }
      })


      Yet, the object won't update and set the status to 'reserved'. Can anyone spot what I'm doing wrong? Should I try to access the particular object using different conditions?



      EDIT : Through trial and error, I think the problem is that the 'find' part returns the parent object, and all of its sub-objects in the 'availability' array are returned. The the 'update' part doesn't know which 'status' to update, and is failing at this point.



      I was returning the date and time from a form in the front end and thought that I could identify the object in the array from it's parent ID plus the date and time of the reservation.



      So I suppose my question should be, as the id returned in the URL is the parent object ID, is there a way to uniquely extract a specified availability object (eg. 5bf2c3bb8b95bd3a7cb3eabe) and update it's status to 'reserved'?



      EDIT 2 : I can also 'find' an item by it's 'availability._id' but the full parent object is returned, so the 'update status' part doesn't work as it's too broad.










      share|improve this question















      I have a data model with nested objects, and I'd like to update the 'available' field to 'reserved' using the main object ID, plus the date and time associated with the nested object. Here's my data...



      {
      "_id": {
      "$oid": "5bf1a675041f242cda138d1c"
      },
      "availability": [
      {
      "_id": {
      "$oid": "5bf2c077d5683c38afacf34b"
      },
      "date": "2018-12-20 ",
      "time": " 11:00 - 12:00 ",
      "status": "reserved"
      },
      {
      "_id": {
      "$oid": "5bf2c3bb8b95bd3a7cb3eabe"
      },
      "date": "2018-11-30",
      "time": "18:00 - 19:00",
      "status": "available"
      }
      ],
      "name": "blah",
      "price": 50,
      "city": "blah",
      "__v": 0
      }


      And the code I'm trying...



      router.get('/reserve/:id', function(req, res, next){

      //I've other stuff here that sets the correct variables according to the console.log, so str2[0] contains the date I want to find, etc.


      Pitch.findOneAndUpdate({
      id: req.params.id,
      'availability.date': 'str2[0]', //passing in 2018-11-30
      'availability.time': 'str2[1]', //passing in 18:00 - 19:00
      'availability.status': 'available'
      },
      {$set: {
      'availability.$.status': 'reserved'
      }
      })


      Yet, the object won't update and set the status to 'reserved'. Can anyone spot what I'm doing wrong? Should I try to access the particular object using different conditions?



      EDIT : Through trial and error, I think the problem is that the 'find' part returns the parent object, and all of its sub-objects in the 'availability' array are returned. The the 'update' part doesn't know which 'status' to update, and is failing at this point.



      I was returning the date and time from a form in the front end and thought that I could identify the object in the array from it's parent ID plus the date and time of the reservation.



      So I suppose my question should be, as the id returned in the URL is the parent object ID, is there a way to uniquely extract a specified availability object (eg. 5bf2c3bb8b95bd3a7cb3eabe) and update it's status to 'reserved'?



      EDIT 2 : I can also 'find' an item by it's 'availability._id' but the full parent object is returned, so the 'update status' part doesn't work as it's too broad.







      node.js mongodb express mongoose






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 20:07

























      asked Nov 19 at 15:06









      S-R

      65




      65
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Pitch.findOneAndUpdate({
          id: req.params.id,
          'availability.status': 'available'
          },
          {$set: {
          'availability.$.status': 'reserved',
          'availability.$.date': 'str2[0]', //passing in 2018-11-30
          'availability.$.time': 'str2[1]', //passing in 18:00 - 19:00
          }
          })





          share|improve this answer





















          • Thanks for the suggestion, but I don't think this is what I need as I'm trying to update the status in the relevant availability._id. I'll edit my original post
            – S-R
            Nov 19 at 19:20











          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',
          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%2f53377454%2fupdating-a-field-in-a-nested-object-when-multiple-conditions-are-met-using-mongo%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








          up vote
          0
          down vote













          Pitch.findOneAndUpdate({
          id: req.params.id,
          'availability.status': 'available'
          },
          {$set: {
          'availability.$.status': 'reserved',
          'availability.$.date': 'str2[0]', //passing in 2018-11-30
          'availability.$.time': 'str2[1]', //passing in 18:00 - 19:00
          }
          })





          share|improve this answer





















          • Thanks for the suggestion, but I don't think this is what I need as I'm trying to update the status in the relevant availability._id. I'll edit my original post
            – S-R
            Nov 19 at 19:20















          up vote
          0
          down vote













          Pitch.findOneAndUpdate({
          id: req.params.id,
          'availability.status': 'available'
          },
          {$set: {
          'availability.$.status': 'reserved',
          'availability.$.date': 'str2[0]', //passing in 2018-11-30
          'availability.$.time': 'str2[1]', //passing in 18:00 - 19:00
          }
          })





          share|improve this answer





















          • Thanks for the suggestion, but I don't think this is what I need as I'm trying to update the status in the relevant availability._id. I'll edit my original post
            – S-R
            Nov 19 at 19:20













          up vote
          0
          down vote










          up vote
          0
          down vote









          Pitch.findOneAndUpdate({
          id: req.params.id,
          'availability.status': 'available'
          },
          {$set: {
          'availability.$.status': 'reserved',
          'availability.$.date': 'str2[0]', //passing in 2018-11-30
          'availability.$.time': 'str2[1]', //passing in 18:00 - 19:00
          }
          })





          share|improve this answer












          Pitch.findOneAndUpdate({
          id: req.params.id,
          'availability.status': 'available'
          },
          {$set: {
          'availability.$.status': 'reserved',
          'availability.$.date': 'str2[0]', //passing in 2018-11-30
          'availability.$.time': 'str2[1]', //passing in 18:00 - 19:00
          }
          })






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 19 at 18:42









          Raunik Singh

          714




          714












          • Thanks for the suggestion, but I don't think this is what I need as I'm trying to update the status in the relevant availability._id. I'll edit my original post
            – S-R
            Nov 19 at 19:20


















          • Thanks for the suggestion, but I don't think this is what I need as I'm trying to update the status in the relevant availability._id. I'll edit my original post
            – S-R
            Nov 19 at 19:20
















          Thanks for the suggestion, but I don't think this is what I need as I'm trying to update the status in the relevant availability._id. I'll edit my original post
          – S-R
          Nov 19 at 19:20




          Thanks for the suggestion, but I don't think this is what I need as I'm trying to update the status in the relevant availability._id. I'll edit my original post
          – S-R
          Nov 19 at 19:20


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53377454%2fupdating-a-field-in-a-nested-object-when-multiple-conditions-are-met-using-mongo%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

          404 Error Contact Form 7 ajax form submitting

          How to know if a Active Directory user can login interactively

          Refactoring coordinates for Minecraft Pi buildings written in Python