38034

How do I save a Mongo Field Value as Long?

Question:

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

This saves "SomeNumberField" as a double. I checked the <a href="https://docs.mongodb.org/manual/core/shell-types/" rel="nofollow">docs, and they recommend "NumberInt"</a>, 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 <a href="https://stackoverflow.com/questions/17185220/mongodb-differences-between-numberlong-and-simple-integer" rel="nofollow">here</a>, still doesn't work.

Answer1:

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.

Answer2:

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 })

Recommend

  • How to Send data from server to clients using select
  • mongodb update matched doc fail
  • MongoDB query with nested structure [duplicate]
  • Listing cities from coordinates in R
  • Do lifetime parameters in `*(&'a mut self)` methods confuse the BorrowChecker?
  • displaying # views on a page without hitting database all the time
  • How many percent of the tweets does twitter sample API give?
  • C++ accessing vector
  • How to best manage SMTP clients
  • Swift iOS don't recognize GCM library imports?
  • How do I check if System::Collections:ArrayList is empty / nullptr / null?
  • Creating a C++ function that calls other Lua function
  • How to enable mapping the private property of the entity
  • Exception creating JSON with LINQ
  • How to use function wrapper in mustache.php?
  • cordova is not defined - cordova.js has already been loaded :: Ionic
  • jQuery: add elements until a particular height is reached
  • Combining two different ActiveRecord collections into one
  • Graphics.CopyFromScreen [Web application] + The handle is invalid
  • Unable to decode certificate at client new X509Certificate2()
  • Declaring variable dynamically in VB.net
  • How to use carriage return with multiple line?
  • Exchange data b/w iOS devices using Bluetooth 4.0
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • Statically linking a C++ library to a C# process using CLI or any other way
  • Release, debug version and Authorization Google?
  • R: gsub and capture
  • AT Commands to Send SMS not working in Windows 8.1
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • Is there a mandatory requirement to switch app.yaml?
  • Comma separated Values
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • apache spark aggregate function using min value
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • Is there any way to bind data to data.frame by some index?
  • Sorting a 2D array using the second column C++
  • How can i traverse a binary tree from right to left in java?
  • How to load view controller without button in storyboard?
  • How do I use LINQ to get all the Items that have a particular SubItem?