Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda