5

我有一个存储在 arangodb 中的对象,它有额外的内部对象,我当前的用例要求我只更新其中一个元素。

存储对象

{
  "status": "Active",
  "physicalCode": "99999",
  "postalCode": "999999",
  "tradingCurrency": "USD",
  "taxRate": "14",
  "priceVatInclusive": "No",
  "type": "eCommerce",
  "name": "John and Sons inc",
  "description": "John and Sons inc",
  "createdDate": "2015-05-25T11:04:14+0200",
  "modifiedDate": "2015-05-25T11:04:14+0200",
  "physicalAddress": "Corner moon and space 9 station",
  "postalAddress": "PO Box 44757553",
  "physicalCountry": "Mars Sector 9",
  "postalCountry": "Mars Sector 9",
  "createdBy": "john.doe",
  "modifiedBy": "john.doe",
  "users": [
    {
      "id": "577458630580",
      "username": "john.doe"
    }
  ],
  "products": [
    {
      "sellingPrice": "95.00",
      "inStock": "10",
      "name": "School Shirt Green",
      "code": "SKITO2939999995",
      "warehouseId": "723468998682"
    },
    {
      "sellingPrice": "95.00",
      "inStock": "5",
      "name": "School Shirt Red",
      "code": "SKITO245454949495",
      "warehouseId": "723468998682"
    },
    {
      "sellingPrice": "95.00",
      "inStock": "10",
      "discount": "5%",
      "name": "School Shirt Blue",
      "code": "SKITO293949495",
      "warehouseId": "723468998682"
    }
  ]
}

我只想更改其中一种产品的库存价值

{
  "sellingPrice": "95.00",
  "inStock": "10",
  "discount": "5%",
  "name": "School Shirt Blue",
  "code": "SKITO293949495",
  "warehouseId": "723468998682"
}

就像 update store product stock less 1 where store id = x 一样,有这样的效果

FOR store IN stores
    FILTER store._key == "837108415472"
    FOR product IN store.products 
        FILTER product.code == "SKITO293949495"
    UPDATE product WITH { inStock: (product.inStock - 1) } IN store.products

除了上述之外,将产品作为单独的文档存储在集合 store_products 中可能是有意义的。我相信 NOSQL 是减少文档大小的最佳方法。

4

1 回答 1

5

找到答案

这里是 arangodb-aql-update-single-object-in-embedded-array和那里 arangodb-aql-update-for-internal-field-of-object

然而,我认为最好维护单独的文档,而不是在检索时使用连接。轻松更新

于 2015-05-27T13:27:23.173 回答