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

83
Visualizações
Mongoose Populate a field

I have two mongoose schema

var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var itemSchema = new Schema({

    name: {type: String, required: true, max: 25, trim: true},
    price: {type: Number, required: true, trim: true, default: 0},
    tax: {
        type: Schema.Types.ObjectId,
        ref: "Store"
    }
});

module.exports = mongoose.model('Item', itemSchema);

The second Schema

var mongoose = require('mongoose');

var Schema = mongoose.Schema;

var storeSchema = new Schema({
    name: {type: String, required: true, trim: true},
    taxes: [
        {
            name: String,
            rate: Number
        }
    ]
});

module.exports = mongoose.model('Store', storeSchema);

What I want to do is populate the itemSchema tax object with the storeSchema taxes array of object. every time I pushed a new tax object to the taxes array mongoose created an ObjectId. I stored that ObjectId in my itemSchema Tax. I want to use that _id to retrieve the store taxes that matches the itemSchema _id.

I have tried this so far, but I get no in the tax attribute.

Item.find().populate("tax", "taxes").exec(function (err, docs) {
        if (err) return console.error(err);
        console.log(items);
    });
over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

Try this query

Item.find().populate({
    path: 'tax',
    select: 'taxes'
}).exec(function (err, docs) {
    if (err) {
        console.error(err);
    } else {
        console.log(docs);
    }
});
over 4 years ago · Santiago Trujillo Relatório

0

Item.find().populate(path:"tax", model: )

Mention your item model file name... don't use "" or '' for the file name, simply add the file name.

over 4 years ago · Santiago Trujillo Relatório

0

Use Item.find().populate("Store", "taxes") instead of Item.find().populate("tax", "taxes").

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