Furkan Başaran Furkan Başaran - 5 months ago 26
JSON Question

cant import json file into mongodb

I have a json file exported from a mongo collection

[
{
"_id": {
"$oid": "53e8a29fe434b85c22000405"
},
"registeredOn": "127.0.0.1",
"password": "5cb67a32022e6df37c6adece17756380",
"email": "asdqwe@hotmail.com",
"clean": "asdqwe",
"username": "asdqwe",
"picture": "default.png",
"message": "yazarın mottosu",
"entry_count": 0,
"point": 0,
"lastLogin": {
"$date": "2014-08-11T14:01:51.102+0300"
},
"generation": 1,
"gender": 0,
"auth": 1,
"registeredAt": {
"$date": "2014-08-11T14:01:51.102+0300"
},
"__v": 0
},
{
"_id": {
"$oid": "53e8a29fe434b85c22000406"
},
"registeredOn": "127.0.0.1",
"password": "7323bac004ed0148c6a3206e0d870ea4",
"email": "asdqwe@gmail.com",
"clean": "asdqweqw",
"username": "asdasdasdo",
"picture": "default.png",
"message": "yazarın mottosu",
"entry_count": 0,
"point": 0,
"lastLogin": {
"$date": "2014-08-11T14:01:51.203+0300"
},
"generation": 1,
"gender": 0,
"auth": 1,
"registeredAt": {
"$date": "2014-08-11T14:01:51.203+0300"
},
"__v": 0
}
]


I get an error from
mongoimport
when I try to import it into my database.

This is the command I used:

mongoimport -d sausozlukdb -c users users.json --jsonArray


result

check 0 0
imported 0 objects
ERROR: encountered 1 error(s)

Please help me, thanks in advance.

I noticed a detail , my export file exported from mongodb 2.6
but my target database is mongodb 2.4

Answer

There have been a number of fixes to mongoimport since MongoDB 2.4 - in particular to improve the robustness of parsing and ensure values can be round-tripped via mongoexport using MongoDB Extended JSON.

Your users.json file is valid, but the 2.4.10 mongoimport definitely doesn't like:

  • UTF characters in the message field
  • $date format

... and possibly more. Since every file tweak seemed to lead to another issue, I did not continue testing.

However, this JSON appears to import fine with the 2.6.4 mongoimport so I would suggest using the newer version instead.

I'm not aware of any issues using 2.6 mongoimport to import into MongoDB 2.4, and it works fine for the example you've provided.