Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoose and field types - Number returns object, String returns string?

Say you define a Mongoose schema like so

aSchema = new Schema
  count: Number
  text: String

A = mongoose.model "A", aSchema

db = mongoose.connect "mongodb://localhost/test"
a = new A
a.count = 99
a.text = "foo"

a.save (err) ->
  A.findById a, (err, a) ->
    console.log typeof a.text, typeof a.count  #prints string, object

Fields of type String behave as expected. But Number fields come back as objects, which means they need to be typecast before being used in comparisons etc.

Why do fields of type Number need casting but not fields of type String?

like image 959
Roxicus Avatar asked Feb 18 '26 22:02

Roxicus


1 Answers

From one of the authors: https://github.com/LearnBoost/mongoose/issues/338#issuecomment-1092330

like image 61
Jack Avatar answered Feb 21 '26 11:02

Jack