3

As per "MongoDB - The definitive guide",

  • JavaScript has only a float type whereas MongoDB has a 4-byte integer, an 8-byte integer and an 8-byte float
  • By default all the numbers will be treated as double

When I tried to insert a integer, I am getting the following:

> n = {"myInteger" : 3 };    
{ "myInteger" : 3 }    
> db.num.insert(n);
> n = db.num.find();    
{ "_id" : ObjectId("4eeee1e5b593471ba5461577"), "myInteger" : 3 }

Based on the above, I have the following questions:

  • How do I insert an 8-byte integer into MongoDB through JavaScript?
  • How do I check the type of the myInteger value displayed above, by JavaScript?
  • How do I check the type of the myInteger value stored in MongoDB?

1 Answer 1

5

How do I insert an 8-byte integer into MongoDB through JavaScript?

You can use

n = {"myLong" :  new NumberLong("123212313")};
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for your other questions, because that seems tricky. You can do instanceof NumberLong, but that won't work for 4-byte numbers, they are both just JS "number".
Thilo: When I retrieve the value from MongoDB, the data is displayed as {"myInteger" : NumberLong(3)} as against {"myInteger" : {"floatApprox : 3} } that is given in the book. Are there two ways of storing 8-byte integers in MongoDB?
[From mongodb.org] Prior to version 1.6, the shell will display {"floatApprox": 3} but from 1.6, the shell will display NumberLong(3).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.