【问题标题】:MongoDB How to insert batch of documents and ignore duplicates c#MongoDB如何插入一批文档并忽略重复c#
【发布时间】:2017-06-02 03:37:17
【问题描述】:

我想插入一批文档,其中一些文档已经存在于集合中。所以我想要要么忽略它们,要么对我来说更好的解决方案是例外我想记录哪个文档是重复的,并在可能的情况下继续插入下一个文档。

我看到了几个类似的问题,但没有一个能解决这个问题。

MongoDB Bulk Insert Ignore Duplicate

MongoDB: how to insert document without repeat

我已经创建了自己的哈希属性,因为我的文档的唯一键将是多个键,所以我将它们累积起来然后计算它的哈希。

我的代码看起来像这样:

        const string connectionString = "mongodb://127.0.0.1/localdb";

        var client = new MongoClient(connectionString);

        _database = client.GetDatabase("localdb");

        var collection = _database.GetCollection<BsonDocument>("Sales");


StringBuilder customValue;

        foreach (var data in dataCollectionDict)
        {
            customValue = new StringBuilder();

            customValue.Append(data["col1"]);
            customValue.Append(data["col2"]);
            customValue.Append(data["col3"]);
            customValue.Append(data["col4"]);
            customValue.Append(data["col5"]);
            customValue.Append(data["col6"]);

            data.AddRange(new BsonDocument("HashMultipleKey", SHA256Func(customValue.ToString())));
        }


await collection.Indexes.CreateOneAsync(new BsonDocument("HashMultipleKey", 1), new CreateIndexOptions() { Unique = true, Sparse = true ,});


await collection.InsertManyAsync(dataCollectionDict);

任何帮助将不胜感激。

【问题讨论】:

    标签: c# mongodb


    【解决方案1】:

    所以这是我找到的解决方法,不确定这是否是最好的解决方案,我很想听听你是否有更好的方法。

          try
            {
                await collection.InsertManyAsync(dataCollectionDict);
            }
            catch (Exception ex)
            {
                ApplicationInsights.Instance.TrackException(ex);
    
                InsertSingleDocuments(dataCollectionDict,collection, dataCollectionQueueMessage);
            }
        }
    
        private static void InsertSingleDocuments(List<BsonDocument> dataCollectionDict, IMongoCollection<BsonDocument> collection
            ,DataCollectionQueueMessage dataCollectionQueueMessage)
        {
            ApplicationInsights.Instance.TrackEvent("About to start insert individual docuemnts and to find the duplicate one");
    
            foreach (var data in dataCollectionDict)
            {
                try
                {
                    collection.InsertOne(data);
                }
                catch (Exception ex)
                {
                    ApplicationInsights.Instance.TrackException(ex,new Dictionary<string, string>() {
                        {
                            "Error Message","Duplicate document was detected, therefore ignoring this document and continuing to insert the next docuemnt"
                        }, {
                            "FilePath",dataCollectionQueueMessage.FilePath
                        }}
                    );
                }
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      • 1970-01-01
      • 2016-03-13
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      相关资源
      最近更新 更多