0

I have a users collection in Mongodb with json schema rules defined. For phone number field i have set bsonType to "number". When a request comes to backend node.js api it parses phone as number. As expected non number strings are parsed as NaN by javascript but mongodb let NaN pass through json schema despite being set to bsonType as number.

app.use("/user/add", (req, res) => {
  const {name} = req.body
  const phone = parseInt(req.body.phone)
  UserCollection.insertOne({name, phone})
})

Document in Mongodb collection

{
  phone: NaN,
  name : "abc",
  _id : "123",
}
FishLegs
  • 221
  • 3
  • 15
  • 1
    NaN (though name (not a number) implies opposite) *is a number! (In javascript)* Why it shouldn't be in bson!? We need a "more clever" solution! – xerx593 Sep 25 '22 at 05:39
  • 1
    Phone numbers: are strings from my XP/p.o.v.! – xerx593 Sep 25 '22 at 05:42
  • 1
    https://stackoverflow.com/questions/2801601/why-does-typeof-nan-return-number/2801617#2801617 – gapsf Sep 25 '22 at 06:13
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#number_type – gapsf Sep 25 '22 at 06:25

0 Answers0