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

218
Views
MongoDB mongoose filter with gender where user in profile is objectID

I am new in backend development. I am trying to create profile endpoints to get all profiles with the gender of the user is either male or female.

Profile contains user as a objectID.

I want to filter profiles using users gender.

My UserSchema looks like this

const userSchema = mongoose.Schema(
  {
    firstname: {
      type: String,
      required: true,
      trim: true,
    },
    lastname: {
      type: String,
      required: true,
      trim: true,
    },
    email: {
      type: String,
      required: true,
      unique: true,
      trim: true,
      lowercase: true,
      validate(value) {
        if (!validator.isEmail(value)) {
          throw new Error('Invalid email');
        }
      },
    },
    password: {
      type: String,
      required: true,
      trim: true,
      minlength: 8,
      validate(value) {
        if (!value.match(/\d/) || !value.match(/[a-zA-Z]/)) {
          throw new Error('Password must contain at least one letter and one number');
        }
      },
      private: true, // used by the toJSON plugin
    },
    role: {
      type: String,
      enum: roles,
      default: 'user',
    },
    gender: {
      type: String,
      enum: genders,
      required: true,
    },
    profileStatus: {
      type: String,
      enum: profileStatus,
      default: 'inProgress',
    },
    isEmailVerified: {
      type: Boolean,
      default: false,
    },
  },
  {
    timestamps: true,
  }
); 

And Profile schema looks like this

const profileSchema = new Schema(
  {
    user: { type: Schema.Types.ObjectId, ref: 'User', required: true, unique: true },

    bio: { type: String, required: true },

    profilePicUrl: {
      type: String,
      required: true,
    },

    birthdate: {
      type: Date,
      required: true,
    },

    profession: {
      type: String,
      required: true,
    },

    profileCompletion: {
      type: Number,
      default: 50,
      min: 0,
      max: 100,
    },

    credits: {
      type: Number,
      default: 2,
      min: 0,
    },

    lastLogin: {
      type: Date,
      default: new Date(),
      required: true,
    },
  },
  { timestamps: true }
);

I want to find profiles where user gender is male or female.

How can I do that?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You can create a endpoint like this and use .find() to find all the users with the gender passed by user

router.get(/user/:gender, async (req, res) => {
    try {
        const users = await User.find({ gender: req.params.gender }).exec();

        res.status(200).json(users);
    } catch (err) {
        return res.status(500);
    }
})
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!