baopham / laravel-dynamodb

Eloquent syntax for DynamoDB
https://packagist.org/packages/baopham/dynamodb
MIT License
484 stars 128 forks source link

How to query List\Map Key-Value field #268

Closed fbramato closed 1 year ago

fbramato commented 1 year ago

Hi, i'm wondering if i can query a table filtering for a List value containing a Map with Key-Value fields.

a query example i would perform would be: scan the table getting the documents having metadata containing (Key = 'id_sofferenza' and Value = '4076115')

I know it's not a problem strictly related to this library but i'm using this to query an external DynamoDB served from a supplier and unfortunately they can't change the model structure.

This is the document:

{
  "id": {
    "S": "7dd76e66-85ee-404a-9951-d60f3a010773"
  },
  "createdAt": {
    "S": "2023-03-08 14:44:55"
  },
  "createdBy": {
    "S": "c07a544a-b43c-11ed-9d19-028998523568"
  },
  "documentType": {
    "S": "identity"
  },
  "extension": {
    "S": "pdf"
  },
  "filename": {
    "S": "CARTA IDENTITA.pdf"
  },
  "metadata": {
    "L": [
      {
        "M": {
          "Key": {
            "S": "documentNumber"
          },
          "Value": {
            "S": "AF34325"
          }
        }
      },
      {
        "M": {
          "Key": {
            "S": "issuingBody"
          },
          "Value": {
            "S": "COMUNE DI MANTOVA"
          }
        }
      },
      {
        "M": {
          "Key": {
            "S": "issuingDate"
          },
          "Value": {
            "S": "2020-05-07"
          }
        }
      },
      {
        "M": {
          "Key": {
            "S": "expiredDate"
          },
          "Value": {
            "S": "2030-05-07"
          }
        }
      },
      {
        "M": {
          "Key": {
            "S": "id_soggetto"
          },
          "Value": {
            "S": "443061"
          }
        }
      },
      {
        "M": {
          "Key": {
            "S": "nr_sofferenza"
          },
          "Value": {
            "S": "4076115"
          }
        }
      },
      {
        "M": {
          "Key": {
            "S": "document_type"
          },
          "Value": {
            "S": "CI"
          }
        }
      },
      {
        "M": {
          "Key": {
            "S": "id_sofferenza"
          },
          "Value": {
            "S": "317646"
          }
        }
      }
    ]
  },
  "mimetype": {
    "S": "application/pdf"
  },
  "slug": {
    "S": "7dd76e66-85ee-404a-9951-d60f3a010773;v2"
  },
  "updatedAt": {
    "S": "2023-03-08 16:34:23"
  },
  "updatedBy": {
    "S": "c07a544a-b43c-11ed-9d19-028998523568"
  },
  "versions": {
    "L": [
      {
        "M": {
          "id": {
            "N": "1"
          },
          "createdAt": {
            "S": "2023-03-08 14:44:55"
          },
          "extension": {
            "S": "pdf"
          },
          "filename": {
            "S": "Carta identità.pdf"
          },
          "mimetype": {
            "S": "application/pdf"
          }
        }
      },
      {
        "M": {
          "id": {
            "N": "2"
          },
          "createdAt": {
            "S": "2023-03-08 16:34:23"
          },
          "extension": {
            "S": "pdf"
          },
          "filename": {
            "S": "CARTA IDENTITA.pdf"
          },
          "mimetype": {
            "S": "application/pdf"
          }
        }
      }
    ]
  }
}

Would appreciate if someone could help with this :)

Thanks!

nelson6e65 commented 1 year ago

I asked to Bing and got this answer (Scan):

This code allows you to get the documents with the documentNumber AF34325, filtering by the attribute of the metadata list.

{
  "TableName": "Documents",
  "FilterExpression": "contains (metadata, :documentNumber)",
  "ExpressionAttributeValues": {
    ":documentNumber": {"M": {"Key": {"S": "documentNumber"}, "Value": {"S": "AF34325"}}}
  }
}

You should add/use an index to use Query instead of Scan. Check if can help somehow 😅

fbramato commented 1 year ago

Truly appreciate, you saved my life and many days of headache!

Thanks!