Population and aggregation not working

Hello there,
I created a project where I have 3 Models:

  1. Attendance
  2. User
  3. Subject

User Schema

const mongoose = require("mongoose");
const validator = require("validator");
const bcrypt = require("bcryptjs");

const UserSchema = new mongoose.Schema(
  {
    name: {
      type: String,
      required: [true, "Please provide name"],
      minlength: [3, "Name can't be less than 3 characters"],
      maxlength: [50, "Name can't be more than 50 characters"],
    },
    email: {
      type: String,
      unique: true,
      required: [true, "Please provide email"],
      validate: {
        validator: validator.isEmail,
        message: "Please provide a valid email",
      },
    },
    password: {
      type: String,
      required: [true, "Please provide password"],
      minlength: 6,
    },
    role: {
      type: String,
      enum: ["student", "admin", "manager", "teacher"],
      default: "student",
    },
    branch: {
      type: String,
      enum: ["IT", "EC", "EE", "CE", "ME"],
      required: true,
    },
    yearOfAdmission: {
      type: Number,
      required: true,
    },
    subjects: [
      {
        type: mongoose.Types.ObjectId,
        ref: "Batch",
      },
    ],
    verificationToken: String,
    isVerified: {
      type: Boolean,
      default: false,
    },
    attendances: [
      {
        type: mongoose.Types.ObjectId,
        ref: "Attendance",
      },
    ],
    verified: Date,
    passwordToken: { type: String },
    passwordTokenExpirationDate: { type: Date },
  },
  {
    timestamps: true,
  }
);

UserSchema.pre("save", async function () {
  if (!this.isModified("password")) return;
  const salt = await bcrypt.genSalt();
  this.password = await bcrypt.hash(this.password, salt);
});

UserSchema.methods.comparePasswords = async function (givenPassword) {
  const matches = await bcrypt.compare(givenPassword, this.password);
  return matches;
};

module.exports = mongoose.model("User", UserSchema);

Batch Schema

const mongoose = require("mongoose");

const BatchSchema = new mongoose.Schema(
  {
    year: {
      type: Number,
      required: true,
    },
    branch: {
      type: String,
      enum: ["IT", "EC", "EE", "ME", "CE"],
      required: true,
    },
    teacher: {
      type: mongoose.Types.ObjectId,
      ref: "User",
      required: true,
    },
    subject: {
      type: String,
      required: true,
    },
    students: [
      {
        type: mongoose.Types.ObjectId,
        ref: "User",
      },
    ],
    attendance: [
      {
        type: mongoose.Types.ObjectId,
        ref: "Attendance",
      },
    ],
    noOfStudents: {
      type: Number,
      default: 0,
    },
  },
  {
    timestamps: true,
  }
);

module.exports = mongoose.model("Batch", BatchSchema);

Attendance Schema

const mongoose = require("mongoose");

const SingleStudentAttendanceSchema = mongoose.Schema({
  student: {
    type: mongoose.Types.ObjectId,
    ref: "User",
  },
  status: {
    type: String,
    enum: ["present", "absent", "holiday", "noclass"],
    default: "noclass",
  },
});

const AttendanceSchema = new mongoose.Schema({
  date: {
    type: String,
    required: true,
  },
  time: {
    type: String,
    required: true,
  },
  typeOfClass: {
    type: String,
    enum: ["theory", "practical"],
    default: "theory",
  },
  subject: {
    type: mongoose.Types.ObjectId,
    ref: "Batch",
  },
  students: [SingleStudentAttendanceSchema],
});

module.exports = mongoose.model("Attendance", AttendanceSchema);

Now I want to fetch data from attendance schema with the following conditions :slight_smile:

  1. On a particular day
  2. I want to find the result for particular teacher

So actually I have teacher id and now I want to populate subject field of attendance schema and from there I’ll get the user id or teacher id and want to match that

Can you please help?