Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何按列表中的值查询DynamoDB筛选_Javascript_Python_Typescript_Amazon Web Services_Amazon Dynamodb - Fatal编程技术网

Javascript 如何按列表中的值查询DynamoDB筛选

Javascript 如何按列表中的值查询DynamoDB筛选,javascript,python,typescript,amazon-web-services,amazon-dynamodb,Javascript,Python,Typescript,Amazon Web Services,Amazon Dynamodb,数据库中有三项: [ { "year": 2013, "info": { "genres": ["Action", "Biography"] } }, { "year": 2013, "info": { "genres": ["Crime",

数据库中有三项:

[
  {
    "year": 2013,
    "info": {
      "genres": ["Action", "Biography"]
    }
  },
  {
    "year": 2013,
    "info": {
      "genres": ["Crime", "Drama", "Thriller"]
    }
  },
  {
    "year": 2013,
    "info": {
      "genres": ["Action", "Adventure", "Sci-Fi", "Thriller"]

    }
  }
]
使用
year
属性作为表的主键,我可以继续使用
FilterExpression
来匹配确切的
列表
[“操作”,“传记”]

与其匹配整个列表,我宁愿通过查询只返回存储在项的
info.genres
字段中的列表中包含字符串“传记”的表项。我想知道是否可以使用DynamoDB
query
API实现这一点

稍后编辑。 工作解决方案(感谢Balu)是使用
QueryFilter
包含
比较运算符:

var params = {
    TableName: TABLE_NAME,
    Limit: 20,
    KeyConditionExpression: "id = :yyyy",
    FilterExpression: `contains(info.genres , :qqqq)`,
    ExpressionAttributeValues: {
      ":qqqq": { S: "Biography" },
      ":yyyy": { N: 2013 },
    },
  }

let promise = docClient.query(params).promise();
promise.then(res => {
console.log("res:", res);
})

简短回答,否。DDB允许存储
key:val
对,因此您要查询的元素应该是顶部元素

答案很长,是的。但是,它使用的是扫描。老实说,就RCU消耗而言,我看不出查询和扫描之间有多大区别。您可以使用
Limit
param来限制RCU在单个网络呼叫中的使用

如果我们现在还不错,你可以在你的过滤器表达式中使用它来实现你想要做的事情。请参阅stack overflow post和github示例

但是,请注意这是一个扫描操作,而不是一个查询,它可能会非常昂贵,因为它不会使用任何索引,并且会迭代表中的每个文档


最好将这些属性提取到顶级文档中,并使用二级索引进行相应的查询。

我们可以在筛选表达式中使用
包含
,而不是
=

因此,
“info.genres=:genres”
可以更改为
包含(info.genres,:gnOne)

AWS仍将在应用筛选器之前在单个查询中查询分区键提取最多1MB的数据。所以,我们将使用相同的RCU(带或不带过滤器表达式),但返回到客户机的数据量将是有限的,所以仍然有用

const dynamodb = new AWS.DynamoDB();
dynamodb.query(
  {
    TableName: "my-test-table",
    Limit: 20,
    KeyConditionExpression: "id = :yyyy",
    FilterExpression: `contains(info.genres , :gnOne)`,
    ExpressionAttributeValues: {
      ":gnOne": { S: "Biography" },
      ":yyyy": { S: "2020" },
    },
  },
  function (err, data) {
    if (err) console.error(err);
    else console.log("dynamodb scan succeeded:", JSON.stringify(data, null, 2));
  }
);
var params = {
    TableName: TABLE_NAME,
    Limit: 20,
    KeyConditionExpression: "id = :yyyy",
    FilterExpression: `contains(info.genres , :qqqq)`,
    ExpressionAttributeValues: {
      ":qqqq": { S: "Biography" },
      ":yyyy": { N: 2013 },
    },
  }

let promise = docClient.query(params).promise();
promise.then(res => {
console.log("res:", res);
})
const dynamodb = new AWS.DynamoDB();
dynamodb.query(
  {
    TableName: "my-test-table",
    Limit: 20,
    KeyConditionExpression: "id = :yyyy",
    FilterExpression: `contains(info.genres , :gnOne)`,
    ExpressionAttributeValues: {
      ":gnOne": { S: "Biography" },
      ":yyyy": { S: "2020" },
    },
  },
  function (err, data) {
    if (err) console.error(err);
    else console.log("dynamodb scan succeeded:", JSON.stringify(data, null, 2));
  }
);