Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

99
Views
Mongoose update subdocument by key

for the following collection:

id: Number
name:[ 
       new Schema({
           language: { type: String, enum: ['en-US', 'fr-CA'] },
           text: String,
       },{ _id: false }
       );
     ],
isActive: Boolean

and sample data like the following:

{
  id:1,
  "name": [
            {"language": "en-US", "text": "Book"},
            {"language": "fr-CA", "text": "livre"}
          ],
   isActive:true
   // and so many other fields
},
{
  id:2,
  "name": [
            {"language": "en-US", "text": "Pen"}
          ],
   isActive:true
   // and so many other fields
}

I would like to update the document by Id:1 and change the text only for french, I tried by:

const input={ "id":"1", "isActive": false}
const name= { "name": [{"language": "fr-CA", "text": "Nouvel article"}]}
await langs.findByIdAndUpdate(
    { _id: input.id },
    { ...input, $addToSet: { name } },
    { new: true, upsert: true }
  );

but the result is: (added another french item)

{
  id:1,
  "name": [
            {"language": "en-US", "text": "Book"},
            {"language": "fr-CA", "text": "livre"},
            {"language": "fr-CA", "text": "Nouvel article"}
          ],
   isActive:false
},

This is based on Brit comment:

https://mongoplayground.net/p/atlw5ZKoYiI

Please advise.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As long as that attribute already exists on the document, you can do something like:

   Collection.findOneAndUpdate({id: 1}, {
        $set: {
          "name.$[elem]": name
        }
      },
      {
        arrayFilters: [  { "elem.language":  name.language  } ],
        upsert: true,
        new: true
      })
about 4 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 Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!