简体   繁体   English

Azure MongoDB API 大容量插入

[英]Azure MongoDB API Bulk Insert

I currently make use of Azure Mongo API for documentDB.我目前将 Azure Mongo API 用于 documentDB。 However this has been causing us many headaches such as inserting documents into the db and trying to ignore the duplicate key errors during an InsertMany() .然而,这给我们带来了很多麻烦,例如将文档插入数据库并试图在InsertMany()期间忽略重复的键错误。 If there is a document within the batch that is a duplicate it immediately throws and error and returns even though I would require it to carry on.如果批处理中有一个重复的文档,它会立即抛出错误并返回,即使我需要它继续执行。 I am aware why we are getting these as I am busy moving data across from a non partitioned collection to a partitioned collection and there is a good chance they already have been migrated.我知道为什么我们会得到这些,因为我正忙于将数据从非分区集合移动到分区集合,并且很有可能它们已经被迁移。

Apparently during the insert many you can pass isOrdered = false within the insertmanyoptions() and it should do this, it did not.显然,在插入过程中,您可以isOrdered = false within the insertmanyoptions()传递isOrdered = false within the insertmanyoptions() ,它应该这样做,但它没有。

After eventually giving up, I attempted to try this via a BulkWriteAsync whilst making use of the IsUpsert = true Sadly I get a funny error:最终放弃后,我尝试通过BulkWriteAsync尝试此BulkWriteAsync同时使用IsUpsert = true可悲的是,我遇到了一个有趣的错误:

Value cannot be null.值不能为空。 Parameter name : requests参数名称:请求

Here is my code below.下面是我的代码。

    public async Task BulkInsertDeviceConfigData(List<DeviceEventConfigure> Elements)
    {
        var models = new List<WriteModel<BsonDocument>>();

        // use ReplaceOneModel with property IsUpsert set to true to upsert whole documents
        foreach (var element in Elements)
        {
            var bsonDoc = element.ToBsonDocument();
            models.Add(new ReplaceOneModel<BsonDocument>(new BsonDocument("_id", element.Id), bsonDoc) { IsUpsert = true });
        };

        await BulkWriteAsync(models as IEnumerable<WriteModel<DeviceEventData>>);
    }

    public virtual async Task BulkWriteAsync(IEnumerable<WriteModel<T>> models)
    {
        await Collection.BulkWriteAsync(models);
    }

在此处输入图片说明

This has been hurting me as we have been increasing our RUs a lot lately to do this even though sadly have been unsuccessful.这一直在伤害我,因为我们最近一直在增加我们的 RU 来做到这一点,尽管遗憾的是没有成功。

Is this an Azure Mongo API issue or is it me?这是 Azure Mongo API 问题还是我的问题?

Maybe your cast也许你的演员

models as IEnumerable<WriteModel<DeviceEventData>> 

isn't working resulting in a null value for the parameter requests.不工作导致参数请求的空值。 I don't think casting to a BsonDocument is necessary, so it can be written as:我不认为强制转换为 BsonDocument 是必要的,所以它可以写成:

public async Task BulkInsertDeviceConfigData(List<DeviceEventConfigure> Elements)
{
    var models = new List<WriteModel<DeviceEventConfigure>>();

    // use ReplaceOneModel with property IsUpsert set to true to upsert whole documents
    foreach (var element in Elements)
    {
        models.Add(new ReplaceOneModel<DeviceEventConfigure>(new BsonDocument("_id", element.Id), element) { IsUpsert = true });
    };

    await BulkWriteAsync(models as IEnumerable<WriteModel<DeviceEventConfigure>>);
}

public virtual async Task BulkWriteAsync(IEnumerable<WriteModel<T>> models)
{
    await Collection.BulkWriteAsync(models);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM