[CSHARP-3896] [IMongoCollection.UpdateOne] Element name is not valid Created: 07/Oct/21  Updated: 27/Oct/23  Resolved: 13/Oct/21

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Unknown
Reporter: Eugene F. Assignee: James Kovacs
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Hello!
I`ve faced the following exception during document updating using ObjectUpdateDefinition.
Is that a bug, or have I just used ObjectUpdateDefinition incorrectly?
If that`s not a bug, the behavior seems very suspicious, because I use ObjectUpdateDefinition in a very intuitive way for EF-experienced ones.
The full source code to reproduce is available here.

Error formatting macro: code: java.lang.StackOverflowError

Unhandled exception. MongoDB.Bson.BsonSerializationException: Element name 'name' is not valid'. at MongoDB.Driver.Core.Connections.BinaryConnection.SendMessagesAsync(IEnumerable`1 messages, MessageEncoderSettings messageEncod erSettings, CancellationToken cancellationToken) at MongoDB.Driver.Core.ConnectionPools.ExclusiveConnectionPool.PooledConnection.SendMessagesAsync(IEnumerable`1 messages, Message EncoderSettings messageEncoderSettings, CancellationToken cancellationToken) at MongoDB.Driver.Core.WireProtocol.CommandUsingCommandMessageWireProtocol`1.ExecuteAsync(IConnection connection, CancellationTok en cancellationToken) at MongoDB.Driver.Core.Servers.Server.ServerChannel.ExecuteProtocolAsync[TResult](IWireProtocol`1 protocol, ICoreSession session, CancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.RetryableWriteOperationExecutor.ExecuteAsync[TResult](IRetryableWriteOperation`1 operation, Ret ryableWriteContext context, CancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.BulkUnmixedWriteOperationBase`1.ExecuteBatchAsync(RetryableWriteContext context, Batch batch, C ancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.BulkUnmixedWriteOperationBase`1.ExecuteBatchesAsync(RetryableWriteContext context, Cancellation Token cancellationToken) at MongoDB.Driver.Core.Operations.BulkMixedWriteOperation.ExecuteBatchAsync(RetryableWriteContext context, Batch batch, Cancellat ionToken cancellationToken) at MongoDB.Driver.Core.Operations.BulkMixedWriteOperation.ExecuteAsync(IWriteBinding binding, CancellationToken cancellationToken ) at MongoDB.Driver.OperationExecutor.ExecuteWriteOperationAsync[TResult](IWriteBinding binding, IWriteOperation`1 operation, Cance llationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl`1.ExecuteWriteOperationAsync[TResult](IClientSessionHandle session, IWriteOperation`1 opera tion, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl`1.BulkWriteAsync(IClientSessionHandle session, IEnumerable`1 requests, BulkWriteOptions opt ions, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionImpl`1.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken) at MongoDB.Driver.MongoCollectionBase`1.UpdateOneAsync(FilterDefinition`1 filter, UpdateDefinition`1 update, UpdateOptions option s, Func`3 bulkWriteAsync) at Sandbox.YetAnother.Program.Main() in C:\Users\Ugenef\RiderProjects\Sandbox\Sandbox.YetAnother\Program.cs:line 30 at Sandbox.YetAnother.Program.<Main>()



 Comments   
Comment by James Kovacs [ 13/Oct/21 ]

Hi, Eugene,

Glad that you got your example working. Yes, ObjectUpdateDefinition<T> can lead to some confusion. Changing the visibility of a public type is a breaking API change. We will consider this and others for the 3.0 release. Thanks for the feedback.

Sincerely,
James

Comment by Eugene F. [ 09/Oct/21 ]

Thank you for the answer, James!

I tried your example code and it works.
I`ve no more questions regarding this issue.

Deleting of `ObjectUpdateDefinition<T>` from the public API (or, at least, explaining its internal purpose in a comment, like Mircosoft devs love to do), perhaps, could be beneficial because people like me who are used to EF-style updates try to look for the familiar behavior in the Mongo Driver and find this one

Thanks again,
Eugene

Comment by James Kovacs [ 08/Oct/21 ]

Hi, evgenyfirstoff@gmail.com,

Thank you for reaching out to us regarding this issue and providing a repro.

Examining the reproduction code, you are using ObjectUpdateDefinition<T> in a way in which it was never intended. This class is used internally when working with wrapped BsonDocuments to specify updates. It was never intended to be used to specify the replacement or updated object, though I can totally understand your confusion. It should likely have never been included as part of the public API.

You can use the Builder API to create the correct `UpdateDefinition<T>` as follows:

var update = Builders<User>.Update
                           .Set(x => x.Age, 3);
await collection.UpdateOneAsync(u => u.Name == "Jack", update);

For more information and examples please see Updates and Update Definition Builder in our documentation.

Please let us know if you have any additional questions.

Sincerely,
James

Generated at Wed Feb 07 21:46:37 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.