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

191
Visualizações
Is there away I can query nexted collections in node js using mongo?

I have a model that Is built like this in node js using mongodb:

// API FOR EDITING Shop
exports.edit = async(req, res)=>{Frients, fruitID, name
    const {fruitTypes, fruitID, name} = req.body
    const parentID = req.params.id 

    // checkin if the fruit type wanted is fruit type list
 await Shop.findById({_id:parentID })
  .exec((error, fruit)=>{
      
  if(error) return res.status(400).json(`error : ${error}`)

if(fruit){
    const fruitt = req.body.fruitTypes.fruit
    // checked if the particular fruit to replace ID is contained in the nested collection 
    const isfruit =   fruit.fruits.find(s=>s._id==fruitID)
    
    if(!isfruit) return res.status(`${fruitTypes.fruit} did't match our records`)

    // find a particular shop ID and replace the nested collects values with given one
    const saved = Shop.findByIdAndUpdate({_id: fruitID}, {
        "$set":{
            "fruitTypes" : fruitTypes
        }
    })
    console.log(saved)
    saved.save()
    res.status(200).json("I tem edited")
    }
  })

}

What I want to achieve is the ability to remove and add fruits in the FruitType [sub collection]. Each Shop belongs to one person and they can put as many types of fruits the want in the fruitType array and can be able to remove only one at atime by replacing the fruit without deleting other fronts on the same FruitType sub collection.

I have tried using FindByIdAndUpdate but it doesn't work for me.

This is my model :

const {Schema, model} = require('mongoose')

const ShopSchema = new Schema({
    
    name:{
        require: true,
       type: String,
    },


    fruitType:[
        {
            fruit:{type: String}
        }
    ],
}

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