Roxicus
Roxicus

Reputation: 109

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?

Upvotes: 3

Views: 1429

Answers (1)

Jack
Jack

Reputation: 9538

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

Upvotes: 1

Related Questions