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

186
Vistas
Mongoose One-to-Many, How to get field value of category

I have 2 collection: Category and book

Book:

      const mongoose = require('mongoose');
        // eslint-disable-next-line camelcase
        const mongoose_delete = require('mongoose-delete');
        
        const { Schema } = mongoose;
        const SchemaTypes = mongoose.Schema.Types;
        const Book = new Schema({
          name: { type: String },
          price: { type: Number },
          images: { type: Array },
          country: { type: String },
          author: { type: String },
          publicationDate: { type: String },
          description: { type: String },
          category: { type: SchemaTypes.ObjectId, ref: 'Category' },
          date: { type: Date, default: Date.now },
        }, {
          timestamps: true,
        });
        Book.plugin(mongoose_delete);
        Book.plugin(mongoose_delete, { overrideMethods: 'all', deletedAt: true });
        module.exports = mongoose.model('Book', Book);

Category:

      const mongoose = require('mongoose');
        // eslint-disable-next-line camelcase
        const mongoose_delete = require('mongoose-delete');
        
        const SchemaTypes = mongoose.Schema.Types;
        const { Schema } = mongoose;
        
        const Category = new Schema({
          name: { type: String, required: true },
          image: { type: String },
          products: [{ type: SchemaTypes.ObjectId, ref: 'Book' }],
          date: { type: Date, default: Date.now },
        }, {
          timestamps: true,
        });
        Category.plugin(mongoose_delete);
        Category.plugin(mongoose_delete, { overrideMethods: 'all', deletedAt: true });
        module.exports = mongoose.model('Category', Category);

and I select all the books received on the list:

      {
        _id: new ObjectId("623668ac5b1d392b37690cbc"),
        name: 'The Godfather',
        price: 110000,
       
        country: 'U.S',
        publicationDate: '1969',
        category: new ObjectId("6238fdf64f60303756b60b20"),
        author: 'Mario Puzo',
        ... : ...

}

And I want to display the category name on the product list, how do I do it? **like: {{book.category.name}}

{{book.category.image}}

?**

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

try $lookup (aggregation)

Book.aggregate([{
    $lookup: {
        from: "Category", 
        localField: "category",
        foreignField: "_id",
        as: "category"
    }
}]).exec(function(err, students) {
   
});
over 4 years ago · Santiago Trujillo Denunciar

0

Since you are using Mongoose, you can use populate() method. You can do it like this:

const books = await Books.find().populate('category')
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