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

1.6K
Views
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 answers
Answer question

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