275
   > db.data.update({'name': 'zero'}, {'$set': {'value': 0}}) 
   > db.data.findOne({'name': 'zero})
    {'name': 'zero', 'value': 0.0}  

How do I get Mongo to insert an integer?

Thank you

Erik Kaplun
  • 37,128
  • 15
  • 99
  • 111
daydreamer
  • 87,243
  • 191
  • 450
  • 722
  • which version of mongodb do you use ? – kamaradclimber Nov 22 '11 at 14:26
  • 1
    https://jira.mongodb.org/browse/SERVER-854 – kamaradclimber Nov 22 '11 at 17:43
  • 1
    note that in my case , when i wanted to {$set:{val:NumberInt(0)}} at some "val" that's already set as Long , it didn't change it. I had to first change it to some other value , then change it back to 0 , for the NumberInt(0) to take effect – kommradHomer Feb 11 '16 at 10:54
  • 1
    I had same problem, after realizing I had mistakenly changed an int32 to double, setting it back using NumberInt() did not fix the type unless I first changed to a different value. – user1055568 Dec 06 '16 at 23:28

4 Answers4

429
db.data.update({'name': 'zero'}, {'$set': {'value': NumberInt(0)}})

You can also use NumberLong.

Bernie Hackett
  • 8,749
  • 1
  • 27
  • 20
  • 1
    but in findOne it says NumberLong(0) not as 0 – daydreamer Nov 21 '11 at 21:41
  • 12
    By default, the mongo shell treats all numbers as floating-point values. So we need to explicitly specify what type of number we want to use e.g. NumberInt or NumberLong. http://docs.mongodb.org/manual/core/shell-types/ – Yadu Sep 19 '13 at 16:31
  • 12
    I have a question. The NumberInt is only provided in mongo shell, how do you do that in JavaScript language like node.js? – Shawyeok Jun 16 '16 at 17:05
  • @Shawyeok here is an answer http://stackoverflow.com/a/21870772/3815843 – GRiMe2D Nov 09 '16 at 06:36
  • 1
    It is important to take into account that ints have bounds. If the number is big consider using NumberLong – Tim Givois Dec 05 '16 at 23:31
  • @GRiMe2D that covers `long`, but not `int`. ~Strangely, I can only find a `Long` type in the Node.js driver...~ oddly, the 32-bit integer type is called `Int32` in the docs (why didn't they call `Long` `Int64` instead?) – Andy Mar 31 '17 at 21:17
25

A slightly simpler syntax (in Robomongo at least) worked for me:

db.database.save({ Year : NumberInt(2015) });
Grid Trekkor
  • 1,443
  • 2
  • 14
  • 19
  • here is my code `getRawPhoneNumber(phoneNumber: string) { let phNumber = phoneNumber.replace(/[^0-9+]/g, ''); return NumberInt(phNumber); }` it says **cannot find `NumberInt`** – Tauqeer Ahmed May 24 '22 at 15:14
7

If the value type is already double, then update the value with $set command can not change the value type double to int when using NumberInt() or NumberLong() function. So, to Change the value type, it must update the whole record.

var re = db.data.find({"name": "zero"})
re['value']=NumberInt(0)
db.data.update({"name": "zero"}, re)
Ethan. Zhang
  • 71
  • 1
  • 2
-14

Well, it's JavaScript, so what you have in 'value' is a Number, which can be an integer or a float. But there's not really a difference in JavaScript. From Learning JavaScript:

The Number Data Type

Number data types in JavaScript are floating-point numbers, but they may or may not have a fractional component. If they don’t have a decimal point or fractional component, they’re treated as integers—base-10 whole numbers in a range of –253 to 253.

Community
  • 1
  • 1
Christian Horsdal
  • 4,914
  • 23
  • 24
  • 30
    Actually that is not true. While JS does not see a difference, as soon as you connect to mongo from another language (e.g. java) you will very much feel the difference. – Omri Spector Jan 13 '13 at 07:50
  • 3
    My application has broken in different points only for one reason, int is saved and returned as float. I suspect the cause must be some manual insertions I did via mongo shell which uses javascript. – Melsi Nov 07 '14 at 12:35