Mongo DB query update nested document

I have this Mongo DB document structure…

{
  "_id": {
    "$oid": "63b6e12d650c56ca2e720c86"
  },
  "userName": "Harshit Gupta ",
  "email": "harshitguptapositive@gmail.com",
  "password": "$2b$10$Ys0AfxbtQKX3XytM1n85dO0dUL2bFAMQiD3w5nSw6zFmda4W4yRn6",
  "tasks": [
    {
      "habbitName": "Jogging",
      "Description": "defended",
      "Sunday": false,
      "Monday": true,
      "Tuesday": false,
      "Wednesday": true,
      "Thursday": false,
      "Friday": true,
      "Saturday": false,
      "Month": [
        {
          "date": 0,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720c8d"
          }
        },
        {
          "date": 1,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720c8e"
          }
        },
        {
          "date": 2,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720c8f"
          }
        },
        {
          "date": 3,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720c90"
          }
        },
        {
          "date": 4,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720c91"
          }
        },
        {
          "date": 5,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720c92"
          }
        },
        {
          "date": 6,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720c93"
          }
        },
        {
          "date": 7,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720c94"
          }
        },
        {
          "date": 8,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720c95"
          }
        },
        {
          "date": 9,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720c96"
          }
        },
        {
          "date": 10,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720c97"
          }
        },
        {
          "date": 11,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720c98"
          }
        },
        {
          "date": 12,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720c99"
          }
        },
        {
          "date": 13,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720c9a"
          }
        },
        {
          "date": 14,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720c9b"
          }
        },
        {
          "date": 15,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720c9c"
          }
        },
        {
          "date": 16,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720c9d"
          }
        },
        {
          "date": 17,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720c9e"
          }
        },
        {
          "date": 18,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720c9f"
          }
        },
        {
          "date": 19,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720ca0"
          }
        },
        {
          "date": 20,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720ca1"
          }
        },
        {
          "date": 21,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720ca2"
          }
        },
        {
          "date": 22,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720ca3"
          }
        },
        {
          "date": 23,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720ca4"
          }
        },
        {
          "date": 24,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720ca5"
          }
        },
        {
          "date": 25,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720ca6"
          }
        },
        {
          "date": 26,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720ca7"
          }
        },
        {
          "date": 27,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720ca8"
          }
        },
        {
          "date": 28,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720ca9"
          }
        },
        {
          "date": 29,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720caa"
          }
        },
        {
          "date": 30,
          "done": false,
          "_id": {
            "$oid": "63b6e14c650c56ca2e720cab"
          }
        }
      ],
      "timeRemind": "23:12",
      "_id": {
        "$oid": "63b6e14c650c56ca2e720c8c"
      },
      "createdAt": {
        "$date": {
          "$numberLong": "1672929612245"
        }
      },
      "updatedAt": {
        "$date": {
          "$numberLong": "1672929612245"
        }
      }
    } ],
      "timeRemind": "03:04",
      "_id": {
        "$oid": "63b7098a4b7294dcdae71128"
      },
      "createdAt": {
        "$date": {
          "$numberLong": "1672939914561"
        }
      },
      "updatedAt": {
        "$date": {
          "$numberLong": "1672939914561"
        }
      }
    }
  ],
  "createdAt": {
    "$date": {
      "$numberLong": "1672929581689"
    }
  },
  "updatedAt": {
    "$date": {
      "$numberLong": "1672939914562"
    }
  },
  "__v": 2
}

I want to first select user id then specific the task id and the document which gets selected then I want to update specific date of the month field can you please suggest the specific query…

first select select user by user id then a task by task id then in Month field i want to update a n element(done attribute to true) whose date is 4.

I tried to apply the following operation.


User.findOneAndUpdate({
      _id: req.user.id,
      tasks:{
        $elemMatch:{
          _id:req.params.taskId,
          Month:{
            $elemMatch:{
              date: dateIndice,
            }
          }
        }
      }
    }
    ,{
      "tasks.$.done": true
    },{
      projection:{
        tasks:{
          $elemMatch:{
            _id: req.params.taskId,
            Month:{
              $elemMatch:{
                date: dateIndice,
              }
            }
          }
        }
      } 
    }
    );

Hi @HARSHIT_GUPTA2 and welcome to the MongoDB community forum!!

Based on the document structure shred above, it seems it is not well formed and thus it makes it difficult for us to reproduce in local environment.
Could you share an updated document which would help to provide the working query?

Adding to that, the example document contains a lot of information in a single document. If this is subjected to grow indefinitely in future, this might make the querying on data difficult and slow. On top of that, MongoDB has a hard limit of 16MB per document that is not configurable, so a document that grows indefinitely can hit this limit.

If your use case allows it, the entire document structure could be split into three different collections as:

  • putting task into its own collection, with username as key, so you can update a single task easily
  • put the login information such as username/password into another collection
  • put the timeremind into another collection as well

Let us know if you have any further questions.

Best Regards
Aasawari