Home > other >  NodeJS and MongoDB - use aggregate and $lookup together with findById
NodeJS and MongoDB - use aggregate and $lookup together with findById

Time:05-20

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

CodePudding user response:

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" });
          }
        });
  • Related