0

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---------
          //------------------


}
2
  • To handle the case where you don't know if it is an update (replace) or in insert (add) try Container.UpsertItemAsync. Commented Mar 15, 2021 at 21:50
  • I tried with UpsertItemAsync which also suggested by Mark, but having issue creating doc Commented Mar 17, 2021 at 0:39

1 Answer 1

2

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"]));
Sign up to request clarification or add additional context in comments.

7 Comments

What about this line: var doc = BsonDocument.Parse(item.ToString());? what is the replacement of BsonDocument ?
Cosmos DB Core (SQL) SDK has no concept of BSON. If you want to write you data into the SQL API you need to translate your data to plain JSON. btw, I assume this is a completely separate Cosmos DB account. You cannot simultaneously read/write data in Cosmos in two data API's.
I had a code for Cosmos DB Mongo API. Now I am converting code for Cosmos DB SQL API. I have already replaced connection properties with CosmosClient for SQL API. So, I am looking to convert rest lines including BSON
Yes, Mark this is separate cosmos db account. so in your code what will be the value of doc?
The doc is the entire document you want to write. Just parse the BSON with the Mongo client to read the document into json, then pass the json to Cosmos.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.