Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

176
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda