[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: |
|
||||||||||||||||
| 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: |
| Comment by auto [ 07/Jul/11 ] |
|
Author: {u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}Message: |
| Comment by auto [ 06/Jul/11 ] |
|
Author: {u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}Message: |
| 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. 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> db.foo.save(doc) PRIMARY> db.foo.find({n: {$type: 1}}) Roundtripping: PRIMARY> delete newdoc._id Regular number will be a double: PRIMARY> db.foo.save(dlbdoc) 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: |
| Comment by auto [ 05/Jul/11 ] |
|
Author: {u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}Message: |
| Comment by auto [ 05/Jul/11 ] |
|
Author: {u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}Message: |
| 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 |
| Comment by auto [ 04/Jul/11 ] |
|
Author: {u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}Message: |
| Comment by auto [ 04/Jul/11 ] |
|
Author: {u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}Message: |
| Comment by auto [ 02/Jul/11 ] |
|
Author: {u'login': u'agirbal', u'name': u'agirbal', u'email': u'antoine@10gen.com'}Message: |
| 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. |
| Comment by Andreas Kalsch [ 11/Apr/11 ] |
|
This is a necessary addition to the NumberLong constructor. |