0

I have an users collection in mongodb, have to find a particular user with mobile number as specified in url query parameter get request.

  1. How to capture mobile number as integer value from query parameters.
  2. How to filter the user based on mobile number captured by query parameter.
nca
  • 397
  • 3
  • 12
  • A few questions: 1. Is there a reason why you'd want a mobile number to be an integer? 2. Are you using Mongoose? – Richard Lovell Jan 19 '20 at 09:28
  • yes I am using mongoose, and I already have an existing user collection in db where mobile_number is stored as Int, and I want to fetch particular user with mobile number specified in query param – nca Jan 19 '20 at 09:32

1 Answers1

0

Something like this (where "User" is a Mongoose model that has a "mobile" field and the request URL has a parameter called "mobile"):

if (req.query.mobile) {
  let mobile = req.query.mobile;
  User.findOne({mobile: mobile})
  .then(user){
     //...
  });
}

The convention for storing phone numbers is to use a String data type. Can you just change the data type? See What's the right way to represent phone numbers?

Richard Lovell
  • 848
  • 10
  • 18
  • 1
    the problem is req.query.mobile; is captured as string and parseInt(req.params.mobileNo) is not helping as well. – nca Jan 19 '20 at 09:41
  • The convention for storing phone numbers is to use a String data type. Can you just change the data type? See https://stackoverflow.com/questions/3483156/whats-the-right-way-to-represent-phone-numbers – Richard Lovell Jan 19 '20 at 09:44