Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing long values into mongodb

How do I import a NumberLong into MongoDB using mongoimport?

Unfortunately, entries like these in a json file cause an error:

{"_id": NumberLong(123)}
{"_id": NumberLong("123")}

Result:

Mon Nov 12 14:41:46 Assertion: 10340:Failure parsing JSON string near: "_id": Num
0xaf6b21 0xabe459 0xabe5dc 0x7b93ad 0x56160d 0x5630f4 0xabb412 0x5546bc 0x7f961b79776d 0x554549 
 mongoimport(_ZN5mongo15printStackTraceERSo+0x21) [0xaf6b21]
 mongoimport(_ZN5mongo11msgassertedEiPKc+0x99) [0xabe459]
 mongoimport() [0xabe5dc]
 mongoimport(_ZN5mongo8fromjsonEPKcPi+0x56d) [0x7b93ad]
 mongoimport(_ZN6Import8parseRowEPSiRN5mongo7BSONObjERi+0xa2d) [0x56160d]
 mongoimport(_ZN6Import3runEv+0x1314) [0x5630f4]
 mongoimport(_ZN5mongo4Tool4mainEiPPc+0x1712) [0xabb412]
 mongoimport(main+0x2c) [0x5546bc]
 /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed) [0x7f961b79776d]
 mongoimport(__gxx_personality_v0+0x419) [0x554549]
Mon Nov 12 14:41:46 exception:BSON representation of supplied JSON is too large: Failure parsing JSON string near: "_id": Num
Mon Nov 12 14:41:46 
Mon Nov 12 14:41:46 imported 0 objects
Mon Nov 12 14:41:46 ERROR: encountered 1 error
like image 319
ghik Avatar asked Nov 12 '12 13:11

ghik


1 Answers

NumberLong is a Javascript Shell output only thing, to show that MongoDB has stored a 64bit integer. For importing, you can just use:

{ "_id": 123 }

And it will automatically either use a normal number (int32) or if it is too large, use a NumberLong (int64). For operations in MongoDB, it doesn't matter whether it is stored as an in32 or an int64.

like image 115
Derick Avatar answered Oct 01 '22 18:10

Derick