Mongoose和字段类型 - Number返回对象,String返回字符串?

时间:2011-09-24 19:08:42

标签: javascript mongodb node.js mongoose

假设你定义一个像这样的Mongoose模式

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

String类型的字段按预期运行。但是Number字段作为对象返回,这意味着它们在用于比较之前需要进行类型转换。

为什么Number类型的字段需要转换而不是String类型的字段?

1 个答案:

答案 0 :(得分:1)