'Export and Import documents with UUID from MongoDB Compass Windows Application

Summary

How to preserve UUIDs when exporting and importing documents?

  1. Export collection as JSON file
  2. Drop collection and re-create collection
  3. Import JSON file
  4. UUIDs are converted to Binary('bsOPwpzDrcKCcE0Iwr1FwrHDn8KRfsKdw7U=', 0)
  5. Cannot read documents from code anymore due to 4.

Details

I needed to remove a field from all documents in a collection, so I thought about exporting all documents in that collection and import them again. During the import, I can deselect the field I wanted to remove, which is exactly what I needed.

However..

The documents in this collection contain UUIDs: one for the ID of the document, one for the ID of another document in another collection.

After exporting, all the UUIDs have been converted to

{
  "$binary": 
  {
    "base64": "QDhscRPCgUAcwqLDosOwPDDDu8KNBA==",
    "subType": "00"
}

When I import the json file back into the collection, all the documents UUIDs are replaced with

Binary('bsOPwpzDrcKCcE0Iwr1FwrHDn8KRfsKdw7U=', 0)

When I try to get those documents from code (C#) I get

An error occurred while deserializing the Id property of class MongoDbGenericRepository.Models.Document: Expected length to be 16, not 29.

Things I tried

When you import a document on MongoDB Compass, you get a dropdown that lets you change the field type: enter image description here

  • I have tried changing that to UUID.
  • I have tried to change the type to UUID as above and also manually edit the "subType": "00" in the JSON export file to both 03 and 04. When I did this, the documents actually had UUID fields instead of Binary('bsOPwpzDrcKCcE0Iwr1FwrHDn8KRfsKdw7U=', 0). However, I still get the same error about the expected length when trying to get those documents from code.
  • I have tried to change the "subType": "00" to both 03 and 04 and keep the field type as Binary. When I did this, the document came up with Binary('bsOPwpzDrcKCcE0Iwr1FwrHDn8KRfsKdw7U=', 3), but I would still get the same error from code.

So now I'm stuck with a bunch of documents that have all the UUIDs messed up and cannot use them anymore :(

Any suggestions on how to fix this? Alternatively, any suggestions on how to backup and restore for this situation?

EDIT:

I was able to export and import one document. Steps:

  1. Copy all the UUIDs of the document to notepad
  2. Export document to JSON file
  3. For each UUID, use this site to convert the UUID to a base64 string
  4. Replace the "base64" value in the JSON file with the string you got in step 3
  5. Import JSON document and set fields that will store those UUIDs to UUID (it will be Binary by default)

This workaround works. However, it becomes unpractical when you have many documents, and even worse if these documents reference other documents by UUID. Therefore, this workaround is not really a solution unfortunately.



Solution 1:[1]

I've had the same problem, and what I did was to edit the document using Robo3T, and then copying/pasting it's contents.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Metalcoder