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

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

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 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