Insertion of integer becomes double as a result of JavaScript

86 views
Skip to first unread message

Russell Bateman

unread,
Sep 17, 2012, 4:15:48 PM9/17/12
to mongodb-user
I must be doing something wrong in my JavaScript. I insert the construct (document) below and the "addresstype" comes back, as seen from the Java driver, as 1.0 or 2.0 instead of as an integer. I'm sure it's the fault of the JavaScript file I'm populating test data with since if I add an address through my application, this doesn't happen. When I do

    this.addresstype = ( Integer ) bson.get( "addresstype" );

In the debugger, I see
 ... "addresstype": 1.0
etc. Obviously, I crash with:

    java.lang.Double cannot be case to java.lang.Integer

How should I write this field in my JS script?

Thanks in advance.


db.accounts.insert(
{
     "password":password_test123,
     "email": "ven...@fun.com",
     "partneroid":partnerx_partneroid,
     "created":created,
     "language":"en",
     "forgotten":false,

     "username":venkatname1,
     "firstname":"Venkat",
     "lastname":"Mynampati",
     "fullname":"Venkat Mynampati",
     "birthdate":ISODate( '1973-01-01 12:12:12' ),
     "phone":"510-987-9874",
     "mobile":"510-987-9874",
     "fax":"510-987-9874",
     "avatar":null,

     "accountdata" :
     {
         "favoritefriend":"Levi Miller",
         "favoritecolor":"white",
         "bestchoice":"selection7",
         "nationalanthem":"String of Pearls",
         "bestbeach":"sandy",
         "menuscheme":"tandy"
     },

     "addresses" :

     [
          {
              "oid":new ObjectId().toString(),
              "addresstype":1,
              "fullname":"Venkat Mynampati",
              "street1":"1234 Mariposa Drive",
              "street2":null,
              "street3":null,
              "city":"Fremont",
              "state":"ca",
              "country":"us",
              "postalcode":"94536",
              "language":"en",
              "lastupdate":created,
              "created":created
          },
          {
              "oid":new ObjectId().toString(),
              "addresstype":2,
              "fullname":"Venkat Mynampati",
              "street1":"P. O. Box 356",
              "street2":null,
              ...


Scott Hernandez

unread,
Sep 17, 2012, 5:41:21 PM9/17/12
to mongod...@googlegroups.com
You want to use NumberInt in javascript, much like NumberLong (for java Long)

http://www.mongodb.org/display/DOCS/Overview+-+The+MongoDB+Interactive+Shell#Overview-TheMongoDBInteractiveShell-Numbers
> --
> You received this message because you are subscribed to the Google
> Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com
> See also the IRC channel -- freenode.net#mongodb

Russell Bateman

unread,
Sep 17, 2012, 6:19:58 PM9/17/12
to mongod...@googlegroups.com
Yes, that's it. Thanks, Scott.
Reply all
Reply to author
Forward
0 new messages