在 C# 中,在两个数组都应该具有唯一性的数组中更新 MongoDB 数组的最佳方法是什么?

问题描述

我目前正在开发一个将缓存 Salesforce 数据的应用程序。 有关数据的所有元数据都保存到 Mongo 集合中,并且通过 Url 提交对其他数据的要求。 由于安全性很重要,因此可以将一个或多个范围附加到每个字段,目的是数据消费者必须将至少一个字段附加到该范围,以便读取数据或执行与这些字段相关的任何搜索

以下将成功地向记录添加新字段并保持它们的唯一性,只要这些字段没有范围:

        public async Task<SObjectConfig> SaveConfigurationAsync(SalesforceDataRequestParameters parameters)
        {
            return await _mongoDatabase.GetCollection<SObjectConfig>(SObjectConfigurationCollection)
                .FindOneAndUpdateAsync<SObjectConfig>(
                    x => x.Name == parameters.sobjectName,Builders<SObjectConfig>.Update
                        .Min(x => x.RefreshIntervalInMinutes,parameters.RefreshIntervalInMinutes)
                        .AddToSetEach(x => x.sobjectFields,parameters.sobjectFieldNames.Select(x => new SObjectFieldConfig(x))),new FindOneAndUpdateOptions<SObjectConfig,SObjectConfig>
                    {
                        IsUpsert = true,ReturnDocument = ReturnDocument.After
                    }
                );
        }

然而,一旦我们在字段中有作用域,具有不同作用域的同一字段的实例将不再相等,因此字段不会是unqiue,相关作用域将分散在记录的多个实例中。

>

我们的输入模型如下所示:

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

        [JsonProperty(PropertyName = "consumingApplication")]
        public string ConsumingApplication { get; set; }

        [JsonProperty(PropertyName = "scope")]
        public string Scope { get; set; }

        [JsonProperty(PropertyName = "sObjectName")]
        public string SObjectName { get; set; }

        [JsonProperty(PropertyName = "sObjectFieldNames")]
        public HashSet<string> SObjectFieldNames { get; set; } = new HashSet<string>();

        [JsonProperty(PropertyName = "indexedFieldNames")]
        public HashSet<string> IndexedFieldNames { get; set; } = new HashSet<string>();

        [JsonProperty(PropertyName = "writableFieldNames")]
        public HashSet<string> WritableFieldNames { get; set; } = new HashSet<string>();

        [JsonProperty(PropertyName = "refreshIntervalInMinutes")]
        public int RefreshIntervalInMinutes { get; set; } = 1440;
    }

我们的 SObject 配置实体如下所示:

    public class SObjectConfig
    {
        [BsonId]
        [BsonElement("Name")]
        public string Name { get; set; }

        [BsonElement("Fields")]
        public List<SObjectFieldConfig> SObjectFields { get; set; }

        [BsonElement("RefreshIntervalInMinutes")]
        public int RefreshIntervalInMinutes { get; set; }

        internal static SObjectConfig From(ISObjectConfigurable sObjectConfig) =>
            new SObjectConfig { 
                Name = sObjectConfig.Name,RefreshIntervalInMinutes = sObjectConfig.RefreshIntervalInMinutes,SObjectFields = sObjectConfig.sobjectFields.ToList()
                    .Select(field => new SObjectFieldConfig(field))
                    .ToList()
            };
    }

字段的实体如下所示:

    public class SObjectFieldConfig
    {
        public SObjectFieldConfig(string name)
        {
            Name = name;
        }

        [BsonElement("Name")]
        public string Name { get; set; }

        [BsonElement("ReadScopes")]
        public List<string> ReadScopes { get; set; }

        [BsonElement("WriteScopes")]
        public List<string> WriteScopes { get; set; }
    }

我想要发生的是:

  1. 当且仅当 parameters.sobjectFieldNames 的旧实例中不存在 SObjectConfig.sobjectFields 中的字段时,才会添加该字段。
  2. 当且仅当 parameters.Scope 未与 SObjectConfig.sobjectFields.ReadScopes 中的字段实例关联时,才会添加
  3. 当且仅当 parameters.Scope 不与 ``SObjectConfig.sobjectFields.WriteScopesand the field is itemized inparameters.WritableFieldNames` 中的字段实例相关联时,才会添加

是否有使用构建器或直接在数据库中实现此目的的好方法? 或者我是否需要将现有记录拉入服务的内存中,以便以更“传统”的方式处理所有逻辑?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)