• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

434
Views
Mongoose how do we update nested objects

I am trying to update nested object using mongoose. I am trying to update the "remindTime date" only and keep the "time" I can update the "remindTime date" however it erases my "remindTime time" property.

I have try the following and none of them work

Model.updateMany(req.query, { remindTime: { date: "2021-09-28" } })

Model.updateMany(req.query, { remindTime: $set:{ { date: "2021-09-28" } } } )

Database data that I am updating

{
    "_id": {
        "$oid": "614d3cedfb2600340fdb28f9"
    },
    "date": "2021-09-23",
    "title": "First test",
    "description": "not working yet",
    "remindTime": {
        "date": "2021-09-28",
        "time": "01:20 am"
    },
    "isComplete": false,
}
almost 3 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Please try this:

Model.updateMany(req.query, { "remindTime.date": "2021-09-28"})
almost 3 years ago · Juan Pablo Isaza Report

0

You can use dot notation:

According to docs:

MongoDB uses the dot notation to access the elements of an array and to access the fields of an embedded document.

To specify or access a field of an embedded document with dot notation, concatenate the embedded document name with the dot (.) and the field name, and enclose in quotes:

"<embedded document>.<field>"

So the query:

Model.updateMany(req.query,
{
  "$set": {
    "remindTime.date": "new date"
  }
})

Example here

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error