开发者

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

开发者 https://www.devze.com 2023-04-07 00:33 出处:网络
Say you define a Mongoose schema like so aSchema = new Schema count: Number text: String A = mongoose.model \"A\", aSchema

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 et开发者_StackOverflowc.

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消