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

137
Visualizações
Push data inside specific field

Can anyone tell me how I can push data inside group ?

productSchema

    const productSchema = new mongoose.Schema({
    name:{
        type:String
    },
    user:[{ 
        author:{
            type: mongoose.Schema.Types.ObjectId,
            ref: 'user'
        }
    }],
    group:[{
        group:{
        type: mongoose.Schema.Types.ObjectId,
        ref: 'group',
        item:{
            type: mongoose.Schema.Types.ObjectId,
            ref: 'item',
        }
    }
    }],
    price:{
        type:number
    },

});

productSchema.pre<IProduct>(/^find/, function(next) {
    this.populate({
          path: 'user.author',
          select: 'fullName email'
        });
        next();
      });

productSchema.pre<IProductSchema>(/^find/, function(next) {
    this.populate({
        path: 'group.group',
        select: '-__v'
        })
        next();
    });
 productSchema.pre<IProductSchema>(/^find/, function(next) {
        this.populate({
            path: 'group.group.item',
            select: '-__v -group'
        })
            next();
});

I successfully populate group.group but in group.group.item I`m not eaven able to push data, this is my push code.

        const updateProduct = await PRODUCT.findByIdAndUpdate(req.params.id,  {
        $push: {
          group: { group: req.params.groupId}
        }
        }
    );
    const query = await Item.find().where(`group`).equals(req.params.groupId).select('-group id');
 query.forEach(async element => {
        const updateProduct = await PRODUCT.findByIdAndUpdate(req.params.id,  {
            $push: {
                group: {"group.item": 'element._id'}}
            }
            
        );
    });

I spend last few hours trying to found solution but unfortunately I`m not able to found it. Hope someone will be able to help me.

Have a nice day !

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

based on the $push documentation

The $push operator appends a specified value to an array

in the schema item is a object just try like this if you want push a value

item:[{
  type: mongoose.Schema.Types.ObjectId,
  ref: 'item',
}]

if you don't want to change the type of item to a array based on following code find the document with _id and group._id after that update the the item like this

await PRODUCT.findOneAndUpdate(
  {
    _id: req.params.id,
    'group._id': groupId,
  },
  {
    "group.$.item" : "the ID"
  }
);

if you changed type of item

await PRODUCT.findOneAndUpdate(
  {
    _id: req.params.id,
    'group._id': groupId,
  },
  {$push:{
    "group.$.item" : element._id
  }}
);
over 4 years ago · Santiago Trujillo 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