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

174
Views
Adding element inside nested array in mongoose
Server Started at Port 3000...
{
  _id: new ObjectId("61c707e9f4ff040a47d27c3f"),
  username: 'adityaaryam',
  password: '1234',
  nameOfUser: 'Aditya Aryam',
  emailOfUser: 'adityaaryam@gmail.com',
  userAllLists: [
    {
      name: 'Hello',
      items: [],
      _id: new ObjectId("61c70d915448262d1dca1a69")
    },
    {
      name: 'Work',
      items: [],
      _id: new ObjectId("61c70d965448262d1dca1a70")
    },
    {
      name: 'Home Work',
      items: [],
      _id: new ObjectId("61c70d9b5448262d1dca1a79")
    },
    {
      name: 'Hello',
      items: [],
      _id: new ObjectId("61c70e7f5448262d1dca1a84")
    },
    {
      name: 'Play',
      items: [],
      _id: new ObjectId("61c7126a5448262d1dca1a9b")
    },
    {
      name: 'Eat',
      items: [],
      _id: new ObjectId("61c71325b0219e6ce4f57990")
    },
    {
      name: 'Walla',
      items: [],
      _id: new ObjectId("61c7197de9564390d506cbe9")
    }
  ],
  __v: 7
}

This is how my database looks like. I want to push new elements to "items" array which is nested inside the "userAllLists" array using mongoose. How do I implement this?

I have been trying findOneAndUpdate using $push but I am not able to achieve my desriable results.

My Schemas are as follows:

const itemSchema = {
    name: String
};

const customListSchema ={
    name:String,
    items:[itemSchema]
};

const userSchema={
    username: String,
    password: String,
    nameOfUser: String,
    emailOfUser: String,
    userAllLists:  [customListSchema],
};

Thanks in Advance!

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

0

I think $push is the right way to push new elements to nested arrays, you didn't show the code you tried to see if it works or not, at all here is an example based on your schema

User.update({_id: "61c707e9f4ff040a47d27c3f", }, {
  '$push': {
     "userAllLists.$[].items": {name: "test item name"}
  }
});

Note: $[] expressions will push the specified object inside all items arrays that exist in userAllLists

To push the item for only specific userAllLists object you can use the following syntax

User.update({_id: "61c707e9f4ff040a47d27c3f", "usersAllLists._id": "61c70d915448262d1dca1a69"}, {
  '$push': {
     "userAllLists.$.items": {name: "test item name"}
  }
});

this will ensure to push the item object to the specified usersAllLists object which has this id 61c70d915448262d1dca1a69

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!