private static async Task SQLApiRun() { using (ICommonApi sqlApi = new SQLAPI()) { await sqlApi.CreateDatabase(); await sqlApi.CreateCollection(); await sqlApi.CreateItems(); ICommonDocument doc = await sqlApi.ReadItem("XMS-001-FE24C"); Console.WriteLine(doc.MetricValue); var result = sqlApi.ReadItemCollection(); foreach (var item in result) { Console.WriteLine($"Update {item.Id}"); await sqlApi.DeleteItem(item.Id, item.DeviceId); } foreach (var item in result) { Console.WriteLine($"Delete {item.Id}"); await sqlApi.DeleteItem(item.Id, item.DeviceId); } } }
public async Task UpdateItem(ICommonDocument reading) { // Update the document. Partition key is not required, again extracted from the document reading.MetricValue = 104; reading.ReadingTime = DateTime.UtcNow; await client.ReplaceDocumentAsync( UriFactory.CreateDocumentUri(DatabaseName, DocumentCollectionName, reading.Id), reading, new RequestOptions { PartitionKey = new PartitionKey(PartitionValue1) }); }
public async Task UpdateItem(ICommonDocument reading) { var update = new UpdateDefinitionBuilder <MONGODeviceReading>() .Set(s => s.MetricValue, (double)500) .Set(s => s.ReadingTime, DateTime.UtcNow); var collection = GetTasksCollection(); await collection.FindOneAndUpdateAsync(Builders <MONGODeviceReading> .Filter.And( Builders <MONGODeviceReading> .Filter.Eq("_id", reading.Id), Builders <MONGODeviceReading> .Filter.Eq("deviceId", reading.DeviceId) ), update); }
private static async Task MongoApiRun() { /////////////////////////////// ////***** to create a collection with partition key you should create sharded collection *****/// // download MongoDB comunity and install it // run cmd // connect to azure cosmos db using the command from "Quick start"->"Mongo DB shell" // db.runCommand( { shardCollection: "Tasks.coll", key: { deviceId: "hashed" } } ) // where db.runCommand( { shardCollection: "<database name>.<collection name>", key: { <partition key-field-column name>: "hashed" } } ) //////////////////////////////// using (ICommonApi sqlApi = new MongoAPI()) { ////await sqlApi.CreateDatabase(); ////await sqlApi.CreateCollection(); //await sqlApi.CreateItems(); var result = sqlApi.ReadItemCollection(); foreach (var item in result) { Console.WriteLine($"Update {item.Id}"); await sqlApi.UpdateItem(item); ICommonDocument doc = await sqlApi.ReadItem(item.Id); Console.WriteLine(doc.MetricValue); } foreach (var item in result) { Console.WriteLine($"Delete {item.Id}"); await sqlApi.DeleteItem(item.Id, item.DeviceId); } } }