Is there a way to do a transactional query/lookup for different types of documents with a common partitionKey stored in the same container

huangapple go评论79阅读模式
英文:

Is there a way to do a transactional query/lookup for different types of documents with a common partitionKey stored in the same container

问题

I have a CosmosDB container in Azure and it contains documents of three types (typeA, typeB, and typeC). There is only one typeA for every key, and multiple documents of typeB and typeC for each key (ex. typeA is a user profile with an id (which acts as the partitionKey), and typeB is an order made by that user, and typeC is a review written by that user -- typeB and typeC both use the user id as their partitionKey). How do I write a transactional operation to read all documents for a given user id? I'm hoping to do a point lookup of typeA, and a ReadManyAsync for typeB and typeC in a transactional manner.

Example data:

object of typeA

{

userId: 01234
lastPurchase: 01-02-2023

}

object of typeB

{

userId: 01234
purchaseId: 56789
purchaseQuantity: 1

}

object of typeC

{

userId: 01234
lastReviewWritten: 01-02-2023
reviewId: 101112

}

This is what I've tried:

container.Database.BeginTransactionAsync()

issue - Database does not have the BeginTransactionAsync() property

container.CreateTransactionalBatch(new PartitionKey("/beneficiary")) .ReadItem(beneficiary).ExecuteAsync()

issue - as far as I can tell this is only for one type of document at a time. If the type is not an issue for the read, do I just deserialize into typeA, typeB, and typeC in this function (by adding a 'type' property to each data type)?

英文:

I have a CosmosDB container in Azure and it contains documents of three types (typeA, typeB, and typeC). There is only one typeA for every key, and multiple documents of typeB and typeC for each key (ex. typeA is a user profile with an id (which acts as the paritionKey), and typeB is an order made by that user, and typeC is a review written by that user -- typeB and typeC both use the user id as their partitionKey). How do I write a transactional operation to read all documents for a given user id? I'm hoping to do a point lookup of typeA, and a ReadManyAsync for typeB and typeC in a transactional manner.

Example data:

object of typeA

{

userId: 01234
lastPurchase: 01-02-2023

}

object of typeB

{

userId: 01234
purchaseId: 56789
purchaseQuantity: 1

}

object of typeC

{

userId: 01234
lastReviewWritten: 01-02-2023
reviewId: 101112

}

This is what I've tried:

container.Database.BeginTransactionAsync()

issue - Database does not have the BeginTransactionAsync() property

container.CreateTransactionalBatch(new PartitionKey("/beneficiary"))
.ReadItem(beneficiary).ExecuteAsync()

issue - as far as i can tell this is only for one type of document at a time. If the type is not an issue for the read, do I just deserialize into typeA, typeB, and typeC in this function (by adding a 'type' property to each data type)?

答案1

得分: 1

如果您尝试同时读取多个文档,最简单的方法是使用查询。在您的情况下,SELECT * FROM c WHERE c.userId = '01234'

因为您正在处理多个实体,您需要将查询和结果集定义为动态数据类型,然后检查您正在使用的属性以确定实体,并反序列化为其适当的对象。

您可以在这个函数中看到这个示例,QueryCustomerAndSalesOrdersByCustomerId(),来自GitHub上的Cosmic Works示例

实际上,这个示例几乎与您的情况相同,数据是从一个包含一个客户档案文档和该客户所有销售订单的容器中查询的。该容器按customerId进行分区。

英文:

If you are trying to read multiple documents at the same time, the easiest way is to use a query. In your case, SELECT * FROM c WHERE c.userId = '01234'.

Because you are dealing with multiple entities, you will need to define the query and result set as a dynamic datatype, then inspect whatever property you are using to determine the entity, and deserialize into its appropriate object.

You see an example of this in this function here, QueryCustomerAndSalesOrdersByCustomerId() from the Cosmic Works Sample on GitHub.

In fact this example is nearly identical to your scenario in that the data is queried from a container that contains one customer profile document and all of that customer's sales orders. The container is partitioned by customerId.

huangapple
  • 本文由 发表于 2023年6月13日 07:46:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76460904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定