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

113
Views
Add object inside nested array in Mongoose

I am working on a web app (using Mongoose).

This is my Users JSON:

[
  {
    _id:'2819vnablj3490fa',
    name:'Bob',
    subscriptions:[
      {
        _id:'s3989408121',
        plan:'Premium',
        services:[
          {
            _id:'2237fdas2323',
            service_name:'Insurance',
          },
           {
            _id:'37280172918',
            service_name:'Consulting',
          },
            {
            _id:'27847378172',
            service_name:'Consulting',
          }
        ]
      }
    ]
  }
]

In my example I would like to:

  1. Get user with id= 2819vnablj3490fa (Bob)
  2. Get subscription with id = s3989408121
  3. Add a new service ({_id:'1920910212',service_name:'Scouting'}) to services

I tried to do the following:

var new_service = {id:'1920910212',service_name:'Scouting'}

  Label.findOneAndUpdate(
      { _id: "2819vnablj3490fa","subscriptions._id":"s3989408121"},
      { $addToSet: {services:new_service} }
    );

Actually, this is one of the many solutions that I have adopted but I still can't get out of it.

Could u help me ?

about 4 years ago · Juan Pablo Isaza
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!