I have below code which is going to copy records from one container in cosmos db to another container in cosmos db. I am just worried on not following the best practices and below code might time out when querying the whole database to get only result with "ClassID". I would appreciate any suggestion as what should I improve in below code. make it async/sync and how can I enumerate in order to overcome any issues in future. As this is an Azure Function which will run first time from beginning but going forward it will be triggered only when there is any changes in Cosmos Db
namespace MigrateDbWithChangeFeed
{
public static class Function1
{
[FunctionName("Function1")]
public static async Task Run([CosmosDBTrigger(
databaseName: "databaseName",
collectionName: "collectionName",
StartFromBeginning = true,
ConnectionStringSetting = "",
LeaseCollectionName = "leases",
CreateLeaseCollectionIfNotExists = true
)] IReadOnlyList<Document> source, DocumentClient client,
ILogger log )
{
string continuationToken = null;
string collectionUrl = UriFactory.CreateDocumentCollectionUri( "databaseName", "SourceCollection" ).ToString();
do
{
var feed = client.CreateDocumentQuery(
collectionUrl,
new SqlQuerySpec( "select * FROM c WHERE IS_DEFINED(c.ClassId)" ),
new FeedOptions { MaxItemCount = 10, EnableCrossPartitionQuery = true, RequestContinuation = continuationToken } ).AsDocumentQuery();
var result = feed.ExecuteNextAsync().Result;
var itemCount = result.Count;
continuationToken = result.ResponseContinuation;
foreach( var doc in result )
{
var data = (JObject)JsonConvert.DeserializeObject( doc.ToString() );
string partitionkeyValue = string.Concat( data["ClassId"].Value<string>(), "|", data["Project"].Value<string>() );
data.Add( new JProperty( "PartitionKey", partitionkeyValue ) );
await client.CreateDocumentAsync( UriFactory.CreateDocumentCollectionUri(
"SourceDatabase", "TargetCollection" ),
data );
}
}
while( continuationToken != null );
}
}
}
azure-cosmosdbor another tag related to databases. \$\endgroup\$