MongoDB Compass import corrupts documents

1.5k Views Asked by At

Description

It's not the first time I'm facing the issue when importing a collection using MongoDB Compass.

The sequence of actions:

  1. Export the collection from remote MongoDB using MongoDB Compass.

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"
    }
  }
}]
  1. Open my local DB with MongoDB Compass, then:

I'm creating the empty collection to perform import in there -> select collection -> Collection tab -> Import Data -> Select file -> Import


The expectations and the actual result

When that collection is imported, I expect to see the following (just as on my remote DB):

enter image description here

But I see this:

enter image description here

Almost all fields are ignored and some fields that imported are with broken nesting!


The question

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?


What I have tried

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.

Notes (UPDATED)

  • After some investigastions thanks to rickhg12hs, we established that this problem occurs when there is at least field with name "document" in collection documents. If we change the field name "document" to "doc", the import works correctly. Probably MongoDB Compass is using the field name "document" for some internal reason and when an imported document has the same field name, bad things happen. Idk if there are some others "fragile" field names that can lead to the same effect when importing collection with MongoDB Compass.
  • To reproduce the issue you can use my json as a collection for import to Mongo DB with Mongo DB Compass, at least, using the versions of MongoDB/MongoDB Compass I specified below.
  • Software:
    • Remote Mongo DB version: 4.2.7 Community
    • Local Mongo DB version: 4.2.7 Community
    • MongoDB Compass version: 1.35.0 (1.35.0)
0

There are 0 best solutions below