• Home
  • Jobs
  • Courses
  • Questions
  • Teachers
  • For business
  • ES/EN

0

10
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,
}
29 days ago ·

Juan Pablo Isaza

2 answers
Answer question

0

Please try this:

Model.updateMany(req.query, { "remindTime.date": "2021-09-28"})
29 days 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

29 days ago · Juan Pablo Isaza Report
Answer question
Find remote jobs
Loading

Discover the new way to find a job!

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