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

246
Vistas
NodeJS and MongoDB - use aggregate and $lookup together with findById

I want to make a relation between two collections - a book and author collections. If i use only get and display all of my books and integrate the data about author by id it works.

Author schema:

 const AuthorSchema = new mongoose.Schema({
      name: { type: String, required: true },
      surname: { type: String, required: true },
      dateOfBirth: { type: String, required: true },
      countryOfBirth: { type: String, required: true },
      
    });

Book schema:

const BookSchema = new mongoose.Schema({
   owner: { type: String, required: true },
   pagesNo: { type: String, required: true },
   releaseDate: { type: String, required: true }, 
   country: { type: String, required: true },
   authorID: { type: Schema.Types.ObjectId, ref: "Author", required: true }, <-- HERE I NEED DATA ABOUT AUTHOR
});

My express function that works for fetching all data:

 router.get("/", async (req, res) => {
   try {
     let books = await Book.aggregate([
       {
        $lookup: {
          from: "authors",
          localField: "authorID",
          foreignField: "_id",
          as: "author",
        },
      },
    ]);

    res.status(200).json(books);
  } catch (err) {
    res.status(404).json({ success: false, msg: "Book is not found" });
  }
});

But now I want to display that joined data also when i search for a single book by ID (findById()). I got an error status if I use a function like this:

router.get("/:bookId", async (req, res) => {
  try {
    let book= await Book.aggregate([
      {
       $lookup: {
              from: "authors",
              localField: "authorID",
              foreignField: "_id",
              as: "author",
            },
      },
    ]);
    book= book.findById({ _id: req.params.bookId});
    res.status(200).json(book);
  } catch (err) {
    res.status(404).json({ success: false, msg: "Book is not found" });
  }
});

Thank you for your help

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

0

use the $match to find only one book for the same query

const mongoose = require('mongoose');
const ObjectId = mongoose.Types.ObjectId();

    router.get("/:bookId", async (req, res) => {
          try {
            let book= await Book.aggregate([
              {
                $match: { _id : ObjectId("book _id") }
              },
              {
               $lookup: {
                      from: "authors",
                      localField: "authorID",
                      foreignField: "_id",
                      as: "author",
                    },
              },
            ]);
            book= book.findById({ _id: req.params.bookId});
            res.status(200).json(book);
          } catch (err) {
            res.status(404).json({ success: false, msg: "Book is not found" });
          }
        });
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