0

I am storing trees of nodes in a (local) Mongo database, using GUIDs as the EntityId and ParentEntityId for each node. I am trying to retrieve nodes using IAggregateFluentExtensions.GraphLookup(). The call to GraphLookup() is throwing:

MongoDB.Bson.BsonSerializationException : GuidSerializer cannot deserialize a Guid when GuidRepresentation is Unspecified.

Why is it throwing this exception? GUID handling is configured for V3, per the code below. It has been working for storing & retrieving other types in the same database that have GUIDs on them.

Here is my Entity class for each Node:

public class TestEntity
{
    [BsonId]
    public ObjectId Id { get; set;}

    [BsonGuidRepresentation(GuidRepresentation.Standard)]
    public Guid EntityId { get; set; }

    [BsonGuidRepresentation(GuidRepresentation.Standard)]
    public Guid ParentEntityId { get; set; }

    public string Name { get; set; }
}

Here is a simplified version of the repository code, which throws the exception, on the GraphLookup() line.

public async Task<List<object>> DoGraphLookup()
{
    BsonDefaults.GuidRepresentationMode = GuidRepresentationMode.V3;
    BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));

    MongoClientSettings mongoClientSettings = MongoClientSettings.FromConnectionString("mongodb://HOSTNAME:27017");
    MongoClient mongoClient = new MongoClient(mongoClientSettings);

    IMongoDatabase database = mongoClient.GetDatabase("DATABASENAME");

    IMongoCollection<TestEntity> collection = database.GetCollection<TestEntity>("COLLECTIONNAME");

    List<object> result = await collection
        .Aggregate()
        //.Match(c => c.Name == "MatchName")
        .GraphLookup<TestEntity, Guid, Guid, Guid, TestEntity, IEnumerable<TestEntity>, object>(
        from: collection,
        startWith: $"${nameof(TestEntity.EntityId)}",
        connectFromField: nameof(TestEntity.ParentEntityId),
        connectToField: nameof(TestEntity.EntityId),
        @as: "BreadCrumb",
        depthField: "order")
        .ToListAsync();

    return result;
}

I'm using C# .NET Core 3.1, with these Nuget packages in my project:

MongoDB.Bson 2.13.1
MongoDB.Driver 2.13.1
MongoDB.Driver.Core 2.13.1

DB Looks like this:

Database contents

--- EDIT --- Exception stack trace:

MyApplication.Repositories.TreeOfNodesRepository.DoGraphLookup: Outcome: Failed Error Message: MongoDB.Bson.BsonSerializationException : GuidSerializer cannot deserialize a Guid when GuidRepresentation is Unspecified. Stack Trace: at MongoDB.Bson.Serialization.Serializers.GuidSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer1 serializer, BsonDeserializationContext context) at MongoDB.Bson.Serialization.Serializers.ObjectSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer1 serializer, BsonDeserializationContext context) at MongoDB.Bson.Serialization.Serializers.DynamicDocumentBaseSerializer1.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) at MongoDB.Bson.Serialization.Serializers.SerializerBase1.MongoDB.Bson.Serialization.IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) at MongoDB.Bson.Serialization.Serializers.ObjectSerializer.DeserializeDiscriminatedValue(BsonDeserializationContext context, BsonDeserializationArgs args) at MongoDB.Bson.Serialization.Serializers.ObjectSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer1 serializer, BsonDeserializationContext context) at MongoDB.Bson.Serialization.Serializers.EnumerableSerializerBase2.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer1 serializer, BsonDeserializationContext context) at MongoDB.Driver.Core.Operations.AggregateOperation1.CursorDeserializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer1 serializer, BsonDeserializationContext context) at MongoDB.Driver.Core.Operations.AggregateOperation1.AggregateResultDeserializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer1 serializer, BsonDeserializationContext context) at MongoDB.Driver.Core.WireProtocol.CommandUsingCommandMessageWireProtocol1.ProcessResponse(ConnectionId connectionId, CommandMessage responseMessage) at MongoDB.Driver.Core.WireProtocol.CommandUsingCommandMessageWireProtocol1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken) at MongoDB.Driver.Core.Servers.Server.ServerChannel.ExecuteProtocolAsync[TResult](IWireProtocol1 protocol, ICoreSession session, CancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.RetryableReadOperationExecutor.ExecuteAsync[TResult](IRetryableReadOperation1 operation, RetryableReadContext context, CancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.ReadCommandOperation1.ExecuteAsync(RetryableReadContext context, CancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.AggregateOperation1.ExecuteAsync(RetryableReadContext context, CancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.AggregateOperation1.ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken) at MongoDB.Driver.OperationExecutor.ExecuteReadOperationAsync[TResult](IReadBinding binding, IReadOperation1 operation, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl1.ExecuteReadOperationAsync[TResult](IClientSessionHandle session, IReadOperation1 operation, ReadPreference readPreference, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl1.AggregateAsync[TResult](IClientSessionHandle session, PipelineDefinition2 pipeline, AggregateOptions options, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl1.UsingImplicitSessionAsync[TResult](Func2 funcAsync, CancellationToken cancellationToken) at MongoDB.Driver.IAsyncCursorSourceExtensions.ToListAsync[TDocument](IAsyncCursorSource1 source, CancellationToken cancellationToken) at MyApplication.Repositories.TreeOfNodesRepository.DoGraphLookup() in c:\Source\saasem\CodeGenerator\Tests\CodeGenerator.ApplicationStore.Tests\Repositories\GraphcLookupTest.cs:line 45 at NUnit.Framework.Internal.TaskAwaitAdapter.GenericAdapter1.BlockUntilCompleted() at NUnit.Framework.Internal.MessagePumpStrategy.NoMessagePumpStrategy.WaitForCompletion(AwaitAdapter awaiter) at NUnit.Framework.Internal.AsyncToSyncAdapter.Await(Func1 invoke) at NUnit.Framework.Internal.Commands.TestMethodCommand.RunTestMethod(TestExecutionContext context) at NUnit.Framework.Internal.Commands.TestMethodCommand.Execute(TestExecutionContext context) at NUnit.Framework.Internal.Execution.SimpleWorkItem.<>c__DisplayClass4_0.b__0() at NUnit.Framework.Internal.ContextUtils.<>c__DisplayClass1_01.<DoIsolated>b__0(Object _) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) --- End of stack trace from previous location --- at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at NUnit.Framework.Internal.ContextUtils.DoIsolated(ContextCallback callback, Object state) at NUnit.Framework.Internal.ContextUtils.DoIsolated[T](Func1 func) at NUnit.Framework.Internal.Execution.SimpleWorkItem.PerformWork()

4
  • DB looks like this: Commented Aug 23, 2021 at 2:48
  • A stack trace might help others to find a solution to your problem. Commented Aug 24, 2021 at 6:23
  • Thanks Richard, I've added the exception stack trace. Commented Aug 25, 2021 at 1:02
  • Was there any solution to this? I ran in the same problem... Commented Feb 17 at 7:51

1 Answer 1

0

With the help of MongoDB support I was able to solve it by adding the following lines on initialisazion:

BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));

var objectSerializer = new ObjectSerializer(BsonSerializer.LookupDiscriminatorConvention(typeof(object)), GuidRepresentation.Standard);
BsonSerializer.RegisterSerializer(objectSerializer);

All credits go to MongoDB Support!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.