Datatype changes when exporting and reimporting document using compass

Steps to replicate

  1. Create a java entity with at least one field as Long datatype
  2. The Long field is stored as int64
  3. Use Mongo Compass to export the document in JSON format
  4. Import the document in same or other collection
  5. The datatype of said field is not int32

Impact

Mongo int32 maps to Java Integer datatype which results in issue when parsing at Java side

Try mongoexport with –jsonFormat rather than Compass to export. The EJSON should keep type information.

Using mongodump/mongorestore might be more appropriate depending of the use-case.

2 Likes