简体   繁体   中英

C# to upload data to Azure Cosmos DB SQL api

I have following part of code which worked to upload data to CosmosDB Mongo API. Now I am using Cosmos Client to upload document to Cosmos DB SQL API. However, Below lines don't support in SQL api.

        var item = objs[i];
        var doc = BsonDocument.Parse(item.ToString());                    
        //saving all  the documents in cosmos
        listTask.Add(collection.ReplaceOneAsync(
            filter: new BsonDocument("id", doc["id"]),
            options: new ReplaceOptions { IsUpsert = true },
            replacement: doc));

Is it possible to replace for SQL API?

private async Task UploadComplete(List<object> objs)
{
    double total_cycles = (int)((double)objs.Count / BATCH);
    
    var cycle= 0;
    //configuring cosmos connection-SQL
    CosmosClient client = new CosmosClient(cosmos);
    var database = client.GetDatabase(cosmos_database);
    var collection = database.GetContainer(cosmos_collection);

           //works for Cosmos DB Mongo API
            var item = objs[i];
            var doc = BsonDocument.Parse(item.ToString());                    
            //saving all  the documents in cosmos
            listTask.Add(collection.ReplaceOneAsync(
                filter: new BsonDocument("id", doc["id"]),
                options: new ReplaceOptions { IsUpsert = true },
                replacement: doc));

          //Cosmos DB SQL API
          //Code here---------
          //------------------


}

The equivalent function in the Cosmos .NET SDK is UpsertItemAsync().

I'm assuming here your partition key is /id.

listTask.Add(collection.UpsertItemAsync(
    item: doc,
    new PartitionKey(doc["id"]));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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