【问题标题】:How can I query DynamoDB table with Nodejs?如何使用 Nodejs 查询 DynamoDB 表?
【发布时间】:2020-10-29 07:06:08
【问题描述】:

我是 Dynamodb 的新手,我不知道如何查询这个数据库。 该表为ave,结构为:

{
  "abuelaMaterna": {
    "id": "galloselcomandante-gmail.com#8308",
    "nombre": null,
    "placa": 8308
  },
  "abuelaPaterna": {
    "id": "galloselcomandante-gmail.com#8389",
    "nombre": null,
    "placa": 8389
  },
  "abueloMaterno": {
    "id": "galloselcomandante-gmail.com#2874",
    "nombre": "ALI",
    "placa": 2874
  },
  "abueloPaterno": {
    "id": "galloselcomandante-gmail.com#2300",
    "nombre": "MERLOT",
    "placa": 2300
  },
  "almacen": {
    "id": "galloselcomandante-gmail.com#1",
    "nombre": "Inventario",
    "tipo": "inventario"
  },
  "almacenes": [
    {
      "almacen": {
        "id": "galloselcomandante-gmail.com#1",
        "nombre": "Inventario",
        "tipo": "inventario"
      },
      "fecha": 1540872000
    }
  ],
  "color": "Indio",
  "createdAt": 1540911765981,
  "disponibleCruce": true,
  "fechaNacimiento": 1534910400,
  "genero": "macho",
  "id": "12857657-d82e-4021-8a28-66edaa1bffd3",
  "madre": {
    "id": "galloselcomandante-gmail.com#8924",
    "nombre": null,
    "placa": 8924
  },
  "marcajeColiseo": "10",
  "notas": [
    {
      "fecha": 1550588244.267,
      "nota": "Vov"
    },
    {
      "fecha": 1552922635.12,
      "nota": "Maca Lo vió y dijo que es especial no se gasta ya"
    }
  ],
  "owner": "galloselcomandante-gmail.com",
  "ownerPlaca": "galloselcomandante-gmail.com#10669",
  "padre": {
    "id": "galloselcomandante-gmail.com#9127",
    "nombre": null,
    "placa": 9127
  },
  "pesos": [

  ],
  "placa": 10669,
  "tipoTuza": "Peine",
  "updatedAt": 1540911765981,
  "viva": true
}

表格有数千个元素,我想要owner 等于example@gmail.com 的所有项目 我已经在 AWS Docs 中进行了研究,但我不知道如何让它发挥作用。我在 Nodejs 中使用“aws-sdk”,我做了一个“dotClient.scan”,但这还不够

【问题讨论】:

  • 你能贴出不起作用的代码吗?

标签: node.js amazon-dynamodb dynamodb-queries


【解决方案1】:

查看here 了解一些使用 node.js 的示例。您的密钥存在的问题是 ID 看起来是一个复合值。您不能对分区键执行 begin_with() 。您可以使用排序键。因此,您可能需要重新考虑架构设计,将电子邮件作为分区键,将 # 后面的数字作为排序键。在不知道您的访问模式的情况下,很难说这是否会在您的数据库中创建一个热键。

【讨论】:

  • 我无法编辑架构设计,这个应用程序已经在生产中,我现在无事可做。我仍然不知道如何使用当前架构进行查询
  • 你不能。查询仅在您的表键上可用。您必须使用扫描,或添加适用于您的案例的全局二级索引
  • 当我进行扫描时,这并没有返回所有我不知道为什么的项目。你能告诉我正确的方法吗?我就是这样做的:AWS.config.update({ accessKeyId, secretAccessKey, region, endpoint }) var docClient = new AWS.DynamoDB.DocumentClient(); var params = { TableName: "ave", }; docClient.scan(params, (error, result) => { if (error) { console.log('error', error); } else { console.log("Total: ", result.Count, result.ScannedCount) } });
  • 扫描返回的数据大小受到限制。在 1MB 之后,您必须使用分页来获取扫描中的其他项目。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-26
  • 1970-01-01
相关资源
最近更新 更多