Question

the following experiment is finished in a mongodb shell(I could not insert picture)

> db.test.remove()

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

> db.test.find()

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

> typeof db.test.num

object

> 

I wonder why the "typeof" always returns "object" type?

Was it helpful?

Solution

According to http://docs.mongodb.org/manual/core/shell-types/ everything in the shell is of type object. If you want to check if a field is a certain type, you need to use instanceof which will return a bool.

To check the type of a field, and actually get the type. Try the following (tested on 2.4.9):

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

That returns number.

EX:

enter image description here

It should be noted, that this is the type in JS, not necessarily the database.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top