Reputation: 4251
It's not the first time I'm facing the issue when importing a collection using MongoDB Compass.
The sequence of actions:
Select collection to export -> Collection tab -> Export Collection -> Export Full Collection -> Select Fields -> Select output -> Json -> Export
It gives me json output with all fields (here is an example with only one document, the rest are deleted by myself after exporting the full collection, just to paste here):
[{
"_id": {
"$oid": "5fecb5013bc3165ef52597d2"
},
"external_id": "536219641",
"internal_id": "2503dbfe-4ada-11eb-869d-0050560c0060",
"surname": "Surname",
"name": "Name",
"middle_name": "MiddleName",
"birth_date": {
"$date": {
"$numberLong": "593654400000"
}
},
"document": {
"series": "1111",
"number": "2222222"
},
"income": {
"salary_amount": 100000
},
"contacts": [
{
"type": "MOBILE_PHONE_NUMBER",
"isActive": true,
"value": "99966677722"
}
],
"_class": "com.example.domain.Person",
"has_сontracts": true,
"citizenship_alfa_code": "RU",
"marital_status": "OTHER",
"created_at": {
"$date": {
"$numberLong": "1609348353000"
}
}
}]
I'm creating the empty collection to perform import in there -> select collection -> Collection tab -> Import Data -> Select file -> Import
When that collection is imported, I expect to see the following (just as on my remote DB):
But I see this:
Almost all fields are ignored and some fields that imported are with broken nesting!
Why do I experience such behaviour with MongoDB Compass? Is it a bug or something? And how to fix this issue to perform import properly with MongoDB Compass?
Well, I have found the way to perform import properly, but without MongoDB Compass, using mongoimport
from my terminal directly, but with extra option --jsonArray
:
mongoimport --host localhost --db mylocaldb --collection person --drop --file person.json --jsonArray
However if I try mongoimport
without --jsonArray
flag, then it fails with the following message:
2023-02-12T00:36:08.961+0400 connected to: mongodb://localhost/
2023-02-12T00:36:08.968+0400 dropping: mylocaldb.person
2023-02-12T00:36:08.974+0400 Failed: cannot decode array into a primitive.D
2023-02-12T00:36:08.974+0400 0 document(s) imported successfully. 0 document(s) failed to import.
Upvotes: 2
Views: 1752