0

How to fetch data from multiple collections in MongoDB by using a common field and sending data (in those fetched collections) using one response?

Employee Scheema:

   const mongoose = require("mongoose");

const employeeSchema = new mongoose.Schema(
  {
    profilePic: {
      type: String,
    },
    employeeID: {
      type: String,
      required: true,
    },
    employeeFirstName: {
      type: String,
      required: true,
    },
    employeeLastName: {
      type: String,
      required: true,
    },
    birthday: {
      type: String,
    },
    streetNo: {
      type: String,
    },
    city: {
      type: String,
    },
    phoneNumber: {
      type: String,
    },

    jobRole: {
      type: String,
      required: true,
    },
    NIC: {
      type: String,
      required: true,
      unique: true,
    },
    companyEmail: {
      type: String,
      required: true,
      unique: true,
    },
    status: {
      type: String,
      required: true,
    },

    resignDate: {
      type: String,
    },
    jobType: {
      type: String,
      required: true,
    },
    candidateID: {
      type: String,
      required: true,
    },
    teamID: {
      type: String,
    },
    lastSeen: {
      type: String,
    },
    token: {
      type: String,
    },
  },
  { timestamps: true }
);

module.exports = mongoose.model("employee", employeeSchema);

Acadamic Qualification Scheema:

const mongoose = require("mongoose");

const academicQualificaationSchema = new mongoose.Schema(
  {
    employeeID: {
      type: String,
      required: true,
    },

    ordinaryLevelResult: {
      type: Array,
      required: true,
    },
    advancedLevelResults: {
      type: Array,
      required: true,
    },

    achievements: {
      type: Array,
      required: true,
    },
  },
  { timestamps: true }
);

module.exports = mongoose.model(
  "academicQualification",
  academicQualificaationSchema
);

Professional Qualification Scheema:

const mongoose = require("mongoose");

const proffesionalQualificaationSchema = new mongoose.Schema(
  {
    employeeID: {
      type: String,
      required: true,
    },
    degree: {
      type: Array,
      required: true,
    },
    language: {
      type: Array,
      required: true,
    },
    course: {
      type: Array,
      required: true,
    },
  },
  { timestamps: true }
);

module.exports = mongoose.model(
  "proffesionalQualification",
  proffesionalQualificaationSchema
);

Controller:

exports.viewEmployees = async (req, res) => {
  try {
    let accQuali, profQuali;
    const employees = await employeeSchema.find();

    accQuali = await academicQualificaationSchema.find();

    profQuali = await ProffesionalQualificationSchema.find();

    if (employees || accQuali || profQuali) {
      return res.status(200).json({ data: { employees, profQuali, accQuali } });
    } else {
      return res.status(404).json({ message: message });
    }
  } catch (err) {
    return res.status(404).json({ err: err.message });
  }
};

This controller is working properly and sends all data in 3 collections with the use of one response. But, I am comfortable if I will be able to Fetch data separately for each employee.

3
  • please add the schemas, sample input and expected output. maybe $lookup is what you need Commented Apr 8, 2022 at 19:06
  • I edited the question including the schemas as per your request, thank you for responding. Commented Apr 8, 2022 at 19:23
  • Yes, solution was $lookup. Thank you for the support @1sina1 Commented Sep 21, 2022 at 15:02

1 Answer 1

1

If you want to get the data from the three collections for specific employee or employees, you can use an aggregation pipeline with a $lookup stage, as suggested by @1sina1. For example:

db.employee.aggregate([
  {
    $match: {"employeeID": "IDA"}
  },
  {
    $lookup: {
      from: "academicQualification",
      localField: "employeeID",
      foreignField: "employeeID",
      as: "academicQualification"
    }
  },
  {
    $lookup: {
      from: "proffesionalQualification",
      localField: "employeeID",
      foreignField: "employeeID",
      as: "proffesionalQualification"
    }
  }
])

As you can see on the playground

Sign up to request clarification or add additional context in comments.

2 Comments

Is it working for you?
Yes, It is working. Thank you for the support and sorry for the late reply..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.