@EngincanV Thanks for sharing article. It works now.
No it doesn't help. I tried to run some small script as follows but still i getting the same error. I dropped my collections in local mongodb then it worked. How can i fix this to my production now?
public static class MongoDBGUIDMigrations
{
public static async Task Migrate()
{
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
var connectionString = "mongodb://localhost:27017"; // Replace as needed
var dbName = "your-database-name"; // Replace with your DB name
var settings = MongoClientSettings.FromConnectionString(connectionString);
var client = new MongoClient(settings);
var database = client.GetDatabase(dbName);
// STEP 3: Get all collection names
var collections = await database.ListCollectionNamesAsync();
var collectionNames = await collections.ToListAsync();
Console.WriteLine($"Found {collectionNames.Count} collections.");
foreach (var collectionName in collectionNames)
{
Console.WriteLine($"\nš Processing collection: {collectionName}");
// Use BsonDocument to handle dynamic schemas
var collection = database.GetCollection<BsonDocument>(collectionName);
var documents = await collection.Find(FilterDefinition<BsonDocument>.Empty).ToListAsync();
int updatedCount = 0;
foreach (var doc in documents)
{
var id = doc.GetValue("_id", null);
if (id == null)
{
Console.WriteLine("Skipping document without _id");
continue;
}
// Replacing with same document will reserialize with Standard Guid format
var filter = Builders<BsonDocument>.Filter.Eq("_id", id);
await collection.ReplaceOneAsync(filter, doc);
updatedCount++;
}
Console.WriteLine($"ā
Updated {updatedCount} documents in collection: {collectionName}");
}
Console.WriteLine("Migration complete. All documents now use UuidStandard format for Guids.");
}
}