kerbermeister
kerbermeister

Reputation: 4251

MongoDB Compass import corrupts documents

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)

Upvotes: 2

Views: 1752

Answers (0)

Related Questions