MongoDB has a legacy format for UUID which causes problem where the UUID is interpreted differently on different platforms, and the standard UUID solves that exact problem.
However, the C# driver defaults to use legacy format and requires switching to standard format explicitly.
1 |
MongoDefaults.GuidRepresentation = MongoDB.Bson.GuidRepresentation.Standard; |
Moreover, after switching to standard format, you might get Exception due to existing UUID. You can fix that Exception by using the Python script below to migrate legacy UUID to standard UUID while maintaining the same C# GUID translation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import uuid from bson.binary import Binary, STANDARD, CSHARP_LEGACY from bson.codec_options import CodecOptions from pymongo import MongoClient client = MongoClient('localhost', 27017) db = client['my_database'] coll = db.get_collection('my_collection', CodecOptions(uuid_representation=CSHARP_LEGACY)) coll2 = db.get_collection('my_collection_new', CodecOptions(uuid_representation=STANDARD)) # Convert from subtype 3 to subtype 4 for item in coll.find(): print(str(item['_id'])); coll2.insert(item); |
After running the script, you can then replace ‘my_collection’ by ‘my_collection_new’.
Based on your actual need, you may also replace CSHARP_LEGACY with JAVA_LEGACY or PYTHON_LEGACY or other legacy format (http://api.mongodb.org/python/current/api/bson/binary.html).
Hope this help!