Details
-
Bug
-
Resolution: Unresolved
-
Major - P3
-
None
-
2.12.3
-
None
-
None
-
MongoDb Server 4.4.6
Description
Hello,
when using
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));
it is impossible to retrieve documents by id.
Full example that creates a document and tries to find it by the Guid:
using System;
|
using MongoDB.Bson;
|
using MongoDB.Bson.Serialization;
|
using MongoDB.Bson.Serialization.Attributes;
|
using MongoDB.Bson.Serialization.Serializers;
|
using MongoDB.Driver;
|
|
|
namespace MongoDbTestProject
|
{
|
public class TestModel |
{
|
[BsonId]
|
public Guid Id; |
|
|
public string SomeString; |
public int SomeNumber; |
}
|
|
|
internal class Program |
{
|
public static void Main(string[] args) |
{
|
BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard)); |
|
|
MongoClient client = new MongoClient("mongodb://localhost:27017"); |
var database = client.GetDatabase("test_db"); |
var collection = database.GetCollection<TestModel>("tests"); |
|
|
|
|
var id = Guid.NewGuid();
|
var data = new TestModel |
{
|
Id = id,
|
|
|
SomeString = "Test", |
SomeNumber = 123 |
};
|
|
|
collection.InsertOne(data);
|
|
|
var foundData = collection.FindSync(f => f.Id == id).FirstOrDefault();
|
|
|
Console.WriteLine($"Found: {foundData}"); |
}
|
}
|
}
|