Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

129
Views
MongoDB relation between two collections by ID with the Express

I am facing a problem while making a relation between two collections (I am using MEAN stack) I have two collections: Books and Authors

In frontend I want to make a CRUD menu, where I add a new book in the table and then from there i insert a few data about book and then I choose author from the dropdown menu (fetchin data from Authors collection)

So at the end my Book collection needs to have a few data about the book and then inside the object i need an array of data about those author.

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: String, required: true }, <-- HERE I NEED DATA ABOUT AUTHOR
});

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 route: book.ts

router.get("/", async (req, res) => {
  try {
    const books= await Book.find();

    let Author = await Author.find({
      books: { $elemMatch: { _id: books.bookID } },
    });

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

The problem is somewhere inside the find() function.. Is it even a good practice? I want that it can handle a lot of data.

Thanks to everyone! Greetings.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Your Book schema would be like this:

const MongooseSchema = 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: mongoose.Schema.ObjectId,
    ref: 'User',
    required: true,
  },
});

And your Author Schema would remain the same (in order to link both schemas).

Your route would be like this (if you want to search all books along with their author names):

router.get('/', async (req, res) => {
  try {
    const books = await Book.find().populate('authorId');

    res.status(200).json(books);
  } catch (err) {
    res.status(404).json({ success: false, msg: 'Booknot found' });
  }
});

And in case you want to search for books with a specific author id then your route would be like this:

router.get('/', async (req, res) => {
  try {
    const books = await Book.find({ authorId }).populate('authorId');

    res.status(200).json(books);
  } catch (err) {
    res.status(404).json({ success: false, msg: 'Booknot found' });
  }
});
over 4 years ago · Santiago Trujillo Report

0

AuthorID should be type ObjectId, not string.

To join data from other table, you have to use an aggregate with a lookup.

let author = await Author.aggregate([
    {
        $lookup:
        {
            from: "books",
            localField: "_id",
            foreignField: "authorID",
            as: "books"
        }
    }
]);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!