0

I just tried to make a simple CRUD code in dotnet core 3.0 c# as an way to get some dotnet skills after do not touching dotnet scope for years.

What I wanted to achieve: Create CLI to get, list, update/set and delete values in some generic configuration store. I got the local Azure Cosomos DB emularo with "mongo" API interfaces enabled and connectable, and started to code.

Error details:

System.AggregateException: One or more errors occurred. (Command update failed: query in command must target a single shard key.)
 ---> MongoDB.Driver.MongoCommandException: Command update failed: query in command must target a single shard key.
   at MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol`1.ProcessReply(ConnectionId connectionId, ReplyMessage`1 reply)
   at MongoDB.Driver.Core.WireProtocol.CommandUsingQueryMessageWireProtocol`1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Servers.Server.ServerChannel.ExecuteProtocolAsync[TResult](IWireProtocol`1 protocol, CancellationToken cancellationToken)
   at MongoDB.Driver.Core.Operations.RetryableWriteOperationExecutor.ExecuteAsync[TResult](IRetryableWriteOperation`1 operation, RetryableWriteContext context, Cancellatio

When I get this: Inside update code for config values I have:

    var updateBuilder = new UpdateDefinitionBuilder<MongoDbConfigurationDocument>();
                List<UpdateDefinition<MongoDbConfigurationDocument>> updates = new List<UpdateDefinition<MongoDbConfigurationDocument>>();
                if (visibleForPages.HasValue)
                {
                    updates.Add(updateBuilder.Set(a => a.VisibleToPages, entry.VisibleToPages));
                }
                if (!string.IsNullOrWhiteSpace(label)) {
                    updates.Add(updateBuilder.Set(a => a.Label, label));
                }
                if (meaning.HasValue)
                {
                    updates.Add(updateBuilder.Set(a => a.Meaning, meaning.ToString()));
                }
                if (value != null)
                {
                    updates.Add(updateBuilder.Set(a => a.ValueType, entry.ValueType));
                    updates.Add(updateBuilder.Set(a => a.StringValue, entry.StringValue));
                    updates.Add(updateBuilder.Set(a => a.BooleanValue, entry.BooleanValue));
                    updates.Add(updateBuilder.Set(a => a.IntegerValue, entry.IntegerValue));
                    updates.Add(updateBuilder.Set(a => a.FloatValue, entry.FloatValue));
                    updates.Add(updateBuilder.Set(a => a.TimeSpanValue, entry.TimeSpanValue));
                    updates.Add(updateBuilder.Set(a => a.DateTimeValue, entry.DateTimeValue));
                    updates.Add(updateBuilder.Set(a => a.BytesValue, entry.BytesValue));
                }
                var builder = new FilterDefinitionBuilder<MongoDbConfigurationDocument>();
                var filter = builder.Eq(a => a.Id, entry.Id);
                await settingsCollectionData.UpdateOneAsync(filter,updateBuilder.Combine(updates));

UpdateOneAsync fails with error above. Here the POCO class of configuration entry:

    public class MongoDbConfigurationDocument
    {
        [BsonId]
        public ObjectId Id { get; set; }
        [BsonElement("component")]
        public string Component { get; set; }
        [BsonElement("path")]
        public string Path { get; set; }
        [BsonElement("valueType")]
        public string ValueType { get; set; }
        [BsonElement("stringValue"), BsonIgnoreIfNull]
        public string StringValue { get; set; }
        [BsonElement("integerValue"), BsonIgnoreIfNull]
        public int? IntegerValue { get; set; }
        [BsonElement("floatValue"), BsonIgnoreIfNull]
        public double? FloatValue { get; set; }
        [BsonElement("booleanValue"), BsonIgnoreIfNull]
        public bool? BooleanValue { get; set; }
        [BsonElement("bytesValue"), BsonIgnoreIfNull]
        public byte[] BytesValue { get; set; }
        [BsonElement("meaning"), BsonIgnoreIfNull]
        public string Meaning { get; set; }
        [BsonElement("visibleToPages")]
        public bool VisibleToPages { get; internal set; }
        [BsonElement("label"),BsonIgnoreIfNull]
        public string Label { get;  set; }
        [BsonElement("dateTimeValue"), BsonIgnoreIfDefault]
        public DateTime DateTimeValue { get; set; }
        [BsonElement("timeSpanValue"), BsonIgnoreIfDefault]
        public TimeSpan TimeSpanValue { get; internal set; }
    }

The document in Cosmos Db being edited:

{
    "_id": {
        "$oid": "5dd9940b3a2e8c062402f8b5"
    },
    "component": "root",
    "path": "test",
    "valueType": "String",
    "stringValue": "test1234",
    "meaning": "String",
    "visibleToPages": false,
    "id": "5dd9940b3a2e8c062402f8b5",
    "_rid": "WNh0AO9EDaIDAAAAAAAAAA==",
    "_self": "dbs/WNh0AA==/colls/WNh0AO9EDaI=/docs/WNh0AO9EDaIDAAAAAAAAAA==/",
    "_etag": "\"00000000-0000-0000-a23b-25e6fdbb01d5\"",
    "_attachments": "attachments/",
    "_ts": 1574540299
}

How the entry object has been obtained/checked for existence:

var pathFilter = filterBuilder.Eq(a => a.Path, path.ToString()) & filterBuilder.Eq(a => a.Component, component);
            var entries = await settingsCollectionData.FindAsync(pathFilter);
            bool creatingNow = false;
            var entry = await entries.FirstOrDefaultAsync();

I never thought that updating a single document by its id would cause so much struggle I would have ot disturb you all with such question. Thanks for your attention and help.

Kote Isaev
  • 273
  • 4
  • 13
  • 1
    Check this thread : https://stackoverflow.com/questions/50112253/getting-query-in-command-must-target-a-single-shard – Mohit Verma Nov 24 '19 at 06:17
  • So the select would find more than one document without `FirstOrDefaultAsync`? `UpdateOneAsync` might be complaining about that – ntohl Nov 25 '19 at 09:28
  • There is 1 (one) row in the table being tested. I even tried to build filter just as eq of "_id" and an id value from loaded object (ObjectId value). – Kote Isaev Nov 25 '19 at 17:02

0 Answers0