1
db.getCollection('someCollection').update({"SomeNumberField": 1},    
   { $set:
      {
        SomeNumberField: NumberLong(1)
      }
   }, { multi: true })

This saves "SomeNumberField" as a double. I checked the docs, and they recommend "NumberInt", which, I assume, should be an int32, but that doesn't work either.

I am surprised I can't find info on this.

Edit: I have tried:

SomeNumberField: NumberLong("1")

As in the answer here, still doesn't work.

Community
  • 1
  • 1
VSO
  • 11,546
  • 25
  • 99
  • 187

2 Answers2

2

Probably the first time I am answering my own question, but this is ridiculous; You need to delete the field first, and then set it, so:

//Delete your double field - make sure you can identify the records you deleted from later though
  db.getCollection('someCollection')
    .update({"SomeOtherFieldUniquerlyIdentifyingItemsToUpdate": "the value you are looking for"},
    {$unset: {SomeNumberField:1}}, {multi: true})

//Create a NEW field with the int64 value you want
db.getCollection('someCollection').update({"SomeOtherFieldUniquerlyIdentifyingItemsToUpdate": "someValue"},    
       { $set:
          {
            SomeNumberField: NumberLong(1)
          }
       }, { multi: true })
VSO
  • 11,546
  • 25
  • 99
  • 187
  • 1
    This actually did happen once upon a time. It was however a "bug" and has since been fixed. The general case is that you could simply do the `$set` an it would indeed replace the value with the intended type. It was ridiculous and it happened to myself as well. – Neil Lunn Sep 29 '19 at 23:28
0

NumberLong() is valid. Passing in a string arg works. I tried the query below which ran

db.test.insert({long: NumberLong("12345678910")});

Could you post the error you're getting when running your query.

White Bullet
  • 205
  • 2
  • 11
  • Doesn't work. Try it, it doesn't convert it. Just saves as a double. – VSO Apr 27 '16 at 15:33
  • There should be no need to delete the field first as mongo fields are polymorphic. You could try and run a simple update without the multi and see if this works for you. db.test.update( {SomeNumberField:1}, {SomeNumberField: NumberLong("12345678910")}); – White Bullet Apr 27 '16 at 15:37
  • What's the point of a simple update if I need to update several thousand items? – VSO Apr 27 '16 at 15:37
  • a simple update for testing purposes, as there's an incorrect behavior happening somewhere. Once you identify the issue, then you could build a more complex query based on a working version. – White Bullet Apr 27 '16 at 15:40
  • I can't even begin to guess at what the issue might be. Also, why are you doing an insert? I am trying to update a field for an existing document. – VSO Apr 27 '16 at 15:42
  • The insert was mainly to quickly test NumberLong. An update worked as well db.test.update( {SomeNumberField:1}, {SomeNumberField: NumberLong("12345678910")}); – White Bullet Apr 27 '16 at 15:43
  • You have succeeded in updating a double to an int64? The script runs and does nothing for me. – VSO Apr 27 '16 at 15:44