如何在DynamoDB中的数组中过滤对象

发布于 2025-02-02 04:49:49 字数 809 浏览 2 评论 0原文

我正在我的nodejs应用程序中实现查询过滤器。

在建模时,我有此架构:

 "clause": [
  {
   "description": "test1",
   "number": 200
  },
  {
   "description": "test2",
   "number": 201
  },
  {
   "description": "test3",
   "number": 202
  },
 ],

基本上我需要将一系列对象告知到发电机,我需要知道哪个记录包含此信息,在这些信息中我搜索了

我成功地过滤了数组中的一个对象,例如:

const params: QueryCommandInput = {
  TableName: config.CONTRACT_DB,
  KeyConditionExpression: 'pk = :i',
  FilterExpression: 'contains(#clause, :clause)',
  ExpressionAttributeNames: {
    '#clause': 'clause',
  },
  ExpressionAttributeValues: {
    ':i': `user#${user.id}`,
    ':clause': {
      number: 200,
      description: 'test1',
    },
  },
};

但是我有必要知道数字和描述的值,我没有通过仅通知其中一个属性来获得结果。

而且我不知道如何在用户输入多个子句中

任何人成功地查询dynamodb中的对象?

I'm implementing query filters in my nodejs application.

In modeling, I have this schema:

 "clause": [
  {
   "description": "test1",
   "number": 200
  },
  {
   "description": "test2",
   "number": 201
  },
  {
   "description": "test3",
   "number": 202
  },
 ],

Basically I need to inform an array of objects to the dynamo and I need to know which record contains this information in which I searched

I've had success filtering just one object within the array, like this:

const params: QueryCommandInput = {
  TableName: config.CONTRACT_DB,
  KeyConditionExpression: 'pk = :i',
  FilterExpression: 'contains(#clause, :clause)',
  ExpressionAttributeNames: {
    '#clause': 'clause',
  },
  ExpressionAttributeValues: {
    ':i': `user#${user.id}`,
    ':clause': {
      number: 200,
      description: 'test1',
    },
  },
};

But it is necessary for me to know the values ​​of number and description, I failed to get the result by informing only one of the properties.

And I have no idea how I would implement a solution where the user enters multiple clauses

Has anyone had success in querying objects inside arrays in dynamodb?, I didn't find anything relevant here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

塔塔猫 2025-02-09 04:49:49

您无法使用您设计的架构搜索描述。要搜索一个值,该值必须在分区密钥(PK)或sort键(SK)中。您需要更改模式。我建议将您的数组分开,以使数组中的每个项目都是数据库中的一个项目。然后,您可以将PK设置为指向父对象的指针,如果要保留数组中的项目顺序,则将SK设置为数字。

接下来,创建一个用描述为PK的GSI,而SK是您的选择,具体取决于您的需求。然后,您可以通过查询GSI来搜索与说明的确切匹配。如果要进行部分搜索(begins_with),则可以将PK设置为固定值,例如“ clausedEscription”和SK进行描述。

You cannot search by description with the schema you designed. To search for a value, that value must be in the partition key (pk) or sort key (sk). You need to change the schema. I suggest splitting your array so that each item in the array is an item in the database. Then you can set the pk to a pointer to the parent object, and set sk to a number if you want to preserve the order of items in the array.

Next, create a GSI with the description as the pk, and the sk is your choice depending on your needs. Then you can search for an exact match with description by querying the GSI. If you want a partial search (begins_with), you can set the pk to a fixed value such as "clauseDescription", and the sk to description.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文