I tried to leverage the CosmosDB change feed to track what was changed in a document, but I didn't find a way to track the changes to a specific filed.
Hi, I tried to leverage the cosmosDB change feed to track what was changed in a document, but I didn't find a way to track the changes to a specific filed. I had a draft code to get the document updates:
public static class Function1
{
[FunctionName("Function1")]
public static void Run([CosmosDBTrigger(
databaseName: "testdb1",
collectionName: "container1",
ConnectionStringSetting = "cosmosDB",
LeaseCollectionName = "leases",
CreateLeaseCollectionIfNotExists=true)]IReadOnlyList<Document> input,
ILogger log)
{
if (input != null && input.Count > 0)
{
log.LogInformation("Documents modified " + input.Count);
log.LogInformation("First document Id " + input[0].Id);
foreach (var document in input)
{
log.LogInformation(document.ToString());
}
}
}
}
and what I received was a full snapshot of the whole document:
[2022-12-15T05:10:16.474Z] Executing 'Function1' (Reason='New changes on collection container1 at 2022-12-15T05:10:16.4737538Z', Id=...)
[2022-12-15T05:10:16.475Z] Documents modified 1
[2022-12-15T05:10:16.476Z] First document Id replace_with_new_document_id4
[2022-12-15T05:15:02.529Z] {
[2022-12-15T05:15:02.539Z] "id": "replace_with_new_document_id4",
[2022-12-15T05:15:02.581Z] "zipcode": 260007,
[2022-12-15T05:15:02.635Z] "_rid": "jF5sAMiuSV4EAAAAAAAAAA==",
[2022-12-15T05:15:02.644Z] "_self": "dbs/jF5sAA==/colls/jF5sAMiuSV4=/docs/jF5sAMiuSV4EAAAAAAAAAA==/",
[2022-12-15T05:15:02.646Z] "_etag": "\"7001f717-0000-0700-0000-639aac360000\"",
[2022-12-15T05:15:02.647Z] "_attachments": "attachments/",
[2022-12-15T05:15:02.648Z] "_ts": 1671081014,
[2022-12-15T05:15:02.663Z] "_lsn": 62
[2022-12-15T05:15:02.664Z] }
I couldn't tell what was changed during the last table update through the response. Could someone tell me is there a way to get the table changes in a more specific way? Like the zip code was changed from 260000 to 260007?
Thanks!
Simple answer: No, this is not possible.
A possible solution as described here:
If you would like to track updates and be able to replay past updates to an item, we recommend modeling these updates as a series of writes instead.