[SERVER-854] Way to create an integer in javascript Created: 30/Mar/10  Updated: 12/Jul/16  Resolved: 04/Jul/11

Status: Closed
Project: Core Server
Component/s: JavaScript
Affects Version/s: None
Fix Version/s: 1.9.1

Type: New Feature Priority: Minor - P4
Reporter: Keith Branton Assignee: Antoine Girbal
Resolution: Done Votes: 19
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

all


Issue Links:
Depends
is depended on by SERVER-2844 Shell inserts integers as doubles Closed
Related
related to SERVER-2667 Cannot compare different instances of... Closed
Participants:

 Description   

Like http://jira.mongodb.org/browse/SERVER-776 but for integers

We're likely to need to be able to create values of all the types bson supports in the console eventually.



 Comments   
Comment by auto [ 16/Jul/11 ]

Author:

{u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}

Message: SERVER-854: now NumberInt properly kept for v8
Branch: master
https://github.com/mongodb/mongo/commit/c08570d8034469fb9479aaa403ce5f2fd1bca39d

Comment by auto [ 07/Jul/11 ]

Author:

{u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}

Message: SERVER-854: fix segv in v8
Branch: master
https://github.com/mongodb/mongo/commit/30037ebe7cbaabd9c8932e395319b15133e5395c

Comment by auto [ 06/Jul/11 ]

Author:

{u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}

Message: SERVER-854: added tests for storing and roundtripping int values
Branch: master
https://github.com/mongodb/mongo/commit/787d7e44a1f7fd6ca4f441bc723d546db3610490

Comment by Antoine Girbal [ 06/Jul/11 ]

ok so I just unplugged the automatic creation of NumberInt JS object with the bson object is of type NumberInt.
This means that integer in bson become normal native numbers in JS that can be used for normal operations.

Then, if it is serialized back to bson, code checks what was the previous type for the value. If NumberInt, then it converts it back to int in bson. So roundtripping should be ok there.

Still it is now possible to explicitly force a value to be an integer for a doc created from scratch, by using NumberInt constructor.

Create a doc with int value:
PRIMARY> n = NumberInt(5)
5
PRIMARY> doc =

{n: n} { "n" : 5 }

PRIMARY> db.foo.save(doc)
PRIMARY> db.foo.find()

{ "_id" : ObjectId("4e14c98b1c89175b61192d31"), "n" : 5 }

PRIMARY> db.foo.find({n: {$type: 1}})
PRIMARY> db.foo.find({n: {$type: 16}})

{ "_id" : ObjectId("4e14c98b1c89175b61192d31"), "n" : 5 }

Roundtripping:
PRIMARY> newdoc = db.foo.findOne({n: {$type: 16}})

{ "_id" : ObjectId("4e14c98b1c89175b61192d31"), "n" : 5 }

PRIMARY> delete newdoc._id
true
PRIMARY> newdoc.b = "foo"
foo
PRIMARY> db.foo.save(newdoc)
PRIMARY> db.foo.find({n: {$type: 16}})

{ "_id" : ObjectId("4e14c98b1c89175b61192d31"), "n" : 5 } { "_id" : ObjectId("4e14ce3f1c89175b61192d34"), "n" : 5, "b" : "foo" }

Regular number will be a double:
PRIMARY> dlbdoc =

{n: 5} { "n" : 5 }

PRIMARY> db.foo.save(dlbdoc)
PRIMARY> db.foo.find({n: {$type: 16}})

{ "_id" : ObjectId("4e14c98b1c89175b61192d31"), "n" : 5 } { "_id" : ObjectId("4e14ce3f1c89175b61192d34"), "n" : 5, "b" : "foo" }

PRIMARY> db.foo.find()

{ "_id" : ObjectId("4e14c98b1c89175b61192d31"), "n" : 5 } { "_id" : ObjectId("4e14ce3f1c89175b61192d34"), "n" : 5, "b" : "foo" } { "_id" : ObjectId("4e14ca5b1c89175b61192d33"), "n" : 5 }
Comment by auto [ 06/Jul/11 ]

Author:

{u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}

Message: SERVER-854: unplugging NumberInt as being automatically created from bson int values, breaks many behaviors
Branch: master
https://github.com/mongodb/mongo/commit/a057d6d68e7a730bcf4a49dba51d9f617c234f32

Comment by auto [ 05/Jul/11 ]

Author:

{u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}

Message: SERVER-854: reverting back tests
Branch: master
https://github.com/mongodb/mongo/commit/e94b9d40d0834a865de45d89822767904831ba81

Comment by auto [ 05/Jul/11 ]

Author:

{u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}

Message: SERVER-854: fix tsv1.js to use float numbers
Branch: master
https://github.com/mongodb/mongo/commit/087c23c6ffa2f2201bd0e0b573f1856a6f58f54b

Comment by auto [ 04/Jul/11 ]

Author:

{u'login': u'erh', u'name': u'Eliot Horowitz', u'email': u'eliot@10gen.com'}

Message: some NumberInt tests and equality SERVER-854
Branch: master
https://github.com/mongodb/mongo/commit/74cd733b1bcd5d6dba129657a9342613fb852eae

Comment by auto [ 04/Jul/11 ]

Author:

{u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}

Message: SERVER-854: fixing jstest.cpp tests that use integer instead of float
Branch: master
https://github.com/mongodb/mongo/commit/cd7944acd456f4e25b8c76e7e560886e27d321b9

Comment by auto [ 04/Jul/11 ]

Author:

{u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}

Message: SERVER-854: NumberInt implemented in SM
Branch: master
https://github.com/mongodb/mongo/commit/5dcc4f9d49173b5e67909714f3bf39f918d2398a

Comment by auto [ 02/Jul/11 ]

Author:

{u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}

Message: SERVER-854: implementation of NumberInt for V8
Branch: master
https://github.com/mongodb/mongo/commit/eb5c2318c7c953d81386f73489afd59d90e49083

Comment by Keith Branton [ 11/May/11 ]

@Antoine, I'm not sure where exactly you are going with your comment.

The reason I requested this was so that I could use the shell to .find() a record, change the values, and .save() it again.

The problem I was facing was that of the types of values getting changes unexpectedly during the process, which was causing class cast exceptions in my java code. I had to relax the java code to simply treat all numbers as Number and convert to the appropriate type each time they are read - which is a bit smelly, but has been working for over a year now.

I'm not even sure what js interpreter mongo uses by default these days - I thought it was still sm. It seems we need a consistent, and therefore predictable, way of dealing with numbers, however, so we can be sure that type information is preserved if it is important in the application. In an ideal world I'd even be able to print the json represention of a document in the log in my java app and copy/change/paste it into the shell, too, without suffering any loss of type. I expect many other developers who choose strongly-typed languages would feel the same way.

If integers could be wrapped with NumberInt(), and doubles are wrapped in Number(), or NumberDouble() or something similar, I think we'd be pretty-much there and very consistent with NumberLong().

Comment by Antoine Girbal [ 10/May/11 ]

Using v8 as engine, it actually supports int32 natively, and uses it for any non-floating number by default.
These numbers are translated into a NumberInt for the BSON.
I think it's better to have integer by default because it's what user expect and what is used the most.
But trying to think of edge cases where this could be backward breaking (one is if user relies on $type in queries).

Comment by Andreas Kalsch [ 11/Apr/11 ]

This is a necessary addition to the NumberLong constructor.

Generated at Thu Feb 08 02:55:22 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.