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

1.6K
Vistas
How to check if email or phone already exists in mongodb database

I am facing a problem regarding checking of email and phone numbers in my MongoDB database. My code only checks if the email is present in the database but does not respond to the phone.

const express = require("express");
const router = express.Router();

require("../db/conn");
const User = require("../model/userSchema");

router.get("/", (req, res) => {
  res.send(`Hello World from server lolstar`);
});

router.post("/register", async (req, res) => {
  const { name, email, phone, work, password, cpassword } = req.body;

  if (!name || !email || !phone || !work || !password || !cpassword) {
    return res.status(422).json({ error: "Please fill your details" });
    }
    try {
        const userExist = await User.findOne({ email: email }, {phone: phone });
        if (userExist)
        {
             return res
          .status(422)
          .json({ error: "Email or Phone number already exists" });
        }
        
        const user = new User({ name, email, phone, work, password, cpassword });
        
        const userRegister = await user.save();
        if (userRegister)
        {
             res.status(201).json({ message: "User registered successfully" });
            }
      
    } catch (err) {
        console.log(err);
        
  }
  
});

module.exports = router;

I have added my UserSchema file. I don't think it has an error please check if something is wrong here. I want the code to check for both email and phone and then use it for authentication purpose.

const mongoose = require('mongoose');

const userSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true,
    },
    email: {
        type: String,
        required: true,
    },
    phone: {
        type: Number,
        required: true,
    },
    work: {
        type: String,
        required: true,
    },
    password: {
        type: String,
        required: true,
    },
    cpassword: {
        type: String,
        required: true,
    }

})

const User = mongoose.model('USER', userSchema);

module.exports = User;
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You want to check if email OR phone exists in database. Your current query checks if both of them exist. You should use $or operator for your query, like this:

await User.findOne({ "$or": [ { email: email }, { phone: phone} ] });
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