Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

84
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

0

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

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda