MongoDB架构设计 - 启用原子性

时间:2015-04-03 19:03:08

标签: mongodb mongodb-query nosql

我有一个基于以下MongoDB架构的集合,这涉及电子商务交易,我有目的地使用单个文档来实现任何更新的原子性,如下所示。

我将在报告中提供以下三个用例。

  1. 对所有州的所有customerTransactions进行总结。
  2. 对每个州的所有customerTransactions进行单独待定/确认/可用/兑换。
  3. 以兑换状态处理所有customerTransactions的总和,按日期分组。
  4. 用例1& 2对我来说很简单,但对于用例#3,我可以对模式做更多的事情,以使其更容易实现。

        {
          "_id":"id",
          "cuid":"CUIDFromCustomerCollection",
          "customerTransactions":{
    
            {
            "retailer":"www.walmart.com","purchaseAmount":"123.00","cashbackAmount":"12.00","state":"pending","transactionTimestamp":"timestampValue","updateTimestamp":"timestampValue"
            },
            {
              "retailer":"www.walmart.com","purchaseAmount":"123.00","cashbackAmount":"12.00","state":"confirmed","transactionTimestamp":"timestampValue","updateTimestamp":"timestampValue"
            } , 
            {
              "retailer":"www.walmart.com","purchaseAmount":"123.00","cashbackAmount":"12.00","state":"available","transactionTimestamp":"timestampValue","updateTimestamp":"timestampValue"
            } ,
              {
              "retailer":"www.walmart.com","purchaseAmount":"123.00","cashbackAmount":"12.00","state":"redeemed","transactionTimestamp":"timestampValue","updateTimestamp":"timestampValue"
            }
    
    }
    

1 个答案:

答案 0 :(得分:0)

该设计适用于所有三种用例。

对“customerTransactions.state”进行索引将有助于优化。