为什么mongodb typeof总是返回“对象”

时间:2014-02-05 17:47:18

标签: mongodb typeof

以下实验在mongodb shell中完成(我无法插入图片)

> db.test.remove()

> db.test.insert({"_id":1, "num":NumberLong(3)})

> db.test.find()

{ "_id" : 1, "num" : NumberLong(3) }

> typeof db.test.num

object

> 

我想知道为什么“typeof”总是返回“object”类型?

1 个答案:

答案 0 :(得分:1)

根据http://docs.mongodb.org/manual/core/shell-types/,shell中的所有内容都是object类型。如果您想检查字段是否属于某种类型,则需要使用instanceof,这将返回bool

检查字段的类型,并实际获取类型。尝试以下(在2.4.9上测试):

a = db.foo.findOne();
typeof a.bar

返回number

EX:

enter image description here

应该注意,这是JS中的类型,不一定是数据库。

相关问题