Pergunta

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
Foi útil?

Solução

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.

Outras dicas

If you use mongodump, you need to use mongorestore. I accidentally ended up typing in mongoimport on a file that was generated with mongodump. It will fail and say something like this:

Wed Jan  9 16:47:23 exception:BSON representation of supplied JSON is too large: Failure parsing JSON string near: <h2></h2>
Wed Jan  9 16:47:23 
Wed Jan  9 16:47:23 Assertion: 10340:Failure parsing JSON string near: <h3>The He
0xafda51 0xac5399 0xac551c 0x7bbecd 0x561a8d 0x563574 0xac1b8b 0x554b3c 0x3b9801ecdd 0x5549c9 
 mongoimport(_ZN5mongo15printStackTraceERSo+0x21) [0xafda51]
 mongoimport(_ZN5mongo11msgassertedEiPKc+0x99) [0xac5399]
 mongoimport() [0xac551c]
 mongoimport(_ZN5mongo8fromjsonEPKcPi+0x56d) [0x7bbecd]
 mongoimport(_ZN6Import8parseRowEPSiRN5mongo7BSONObjERi+0xa2d) [0x561a8d]
 mongoimport(_ZN6Import3runEv+0x1314) [0x563574]
 mongoimport(_ZN5mongo4Tool4mainEiPPc+0x16cb) [0xac1b8b]
 mongoimport(main+0x2c) [0x554b3c]
 /lib64/libc.so.6(__libc_start_main+0xfd) [0x3b9801ecdd]
 mongoimport(_
_gxx_personality_v0+0x419) [0x5549c9]

Not the response you'd expect, so be careful how you move data in/out of Mongo.

This is old, but well positioned :)

I had some mongo data that I absolutely HAD to import. ISODate and NumberLong must be replaced by their mongo internal equivalents. So I did this:

perl -ape 'BEGIN {$\=""} s/ISODate\((.*)\)/ { "\$date": $1 }/g; s/NumberLong\(\s*\"?(\d+)\"?\s*\)/ { "\$numberLong" : \"$1\" }/g; chop' data.json > data2.json

this was swallowed without a problem by:

mongoimport --db mydb --collection mycoll   --file data2.json  --jsonArray
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top