Answers for "read all items from cosmos container"

C#
0

c# cosmos db add items into container

using Microsoft.Azure.Cosmos

public class Test
{
	[JsonProperty(PropertyName = "CategoryId")]
	public int Id { get; set; }
}

public async Task AddItemsAsync(CosmosClient cosmosClient, Database database, Container container)
{

	Test test = new Test();
	test.Id = 0;

	ItemResponse<Test> testResponse = await container.CreateItemAsync<Test>(test, new PartitionKey("<Your partition key>"));

	Console.WriteLine("Created item in database with id: {0} Operation consumed {1} RUs.n"
		, testResponse.Resource.Id, testResponse.RequestCharge);
}
Posted by: Guest on September-23-2020
0

cosmos db get all items in container

//These three are from microsoft.azure.cosmos
//You can download them using nuget
CosmosClient cosmosClient = new CosmosClient(Uri, PrimaryKey);
Database database = cosmosClient.GetDatabase(DatabaseId);
Container container = database.GetContainer(ContainerId);

string sqlQueryText = "SELECT * FROM c";

QueryDefinition definition = new QueryDefinition(sqlQueryText);

/*
Note that your Test object must have the same properties that
are stored in your database

	public class Test
    {

	[JsonProperty(PropertyName = "<your property name>")]
	public string property { get; set; }
    }
*/

var iterator = container.GetItemQueryIterator<Test>(definition);

while (iterator.HasMoreResults)
{
	FeedResponse<Test> result = await iterator.ReadNextAsync();
	foreach (var item in result)
    {
		Console.Writeline(item.property);
    }
}
Posted by: Guest on September-22-2020

Code answers related to "read all items from cosmos container"

C# Answers by Framework

Browse Popular Code Answers by Language