Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

130
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda