如何在DynamoDB中过滤嵌套数组对象 [英] How to Filter Nested Array Object in DynamoDB
问题描述
我是AWS DynamoDB的初学者,我想使用SENDTO.emailAddress = first@first.com作为FilterExpression扫描DynamoDB。
I am very beginner to AWS DynamoDB, I want to scan the DynamoDB with SENDTO.emailAddress = "first@first.com" as FilterExpression.
数据库结构如下
{
ID
NAME
MESSAGE
SENDTO[
{
name
emailAddress
}
]
}
样本数据
{
ID: 1,
NAME: "HELLO",
MESSAGE: "HELLO WORLD!",
SENDTO: [
{
name: "First",
emailAddress: "first@first.com"
},
{
name: "Second",
emailAddress: "second@first.com"
}
]
}
我想检索与emailAddress匹配的文档。我尝试使用过滤器表达式进行扫描,这是我的代码以检索数据。我正在使用AWS Javascript SDK。
I want to retrieve document that match emailAddress. I tried to scan with filter expression and here is my code to retrieve the data. I am using AWS Javascript SDK.
let params = {
TableName : "email",
FilterExpression: "SENDTO.emailAddress = :emailAddress",
ExpressionAttributeValues: {
":emailAddress": "first@first.com",
}
}
let result = await ctx.docClient.scan(params).promise();
推荐答案
为了通过<$ c $查找项目c> sendto 属性,您需要同时知道 name
和 emailAddress
属性值。 DynamoDB无法仅通过对象中的一个属性(即仅 email
属性值)来查找数据。
In order to find the item by sendto
attribute, you need to know both name
and emailAddress
attribute value. DynamoDB can't find the data by just one of the attributes in an object (i.e. email
attribute value alone).
CONTAINS 函数可用于在List数据类型中查找数据。
CONTAINS function can be used to find the data in List data type.
列表支持CONTAINS :当评估 a CONTAINS b时, a
可以是一个列表;但是, b不能是集合,地图或列表。
CONTAINS is supported for lists: When evaluating "a CONTAINS b", "a" can be a list; however, "b" cannot be a set, a map, or a list.
使用包含以下内容的示例代码:-
var params = {
TableName: "email",
FilterExpression: "contains (SENDTO, :sendToVal)",
ExpressionAttributeValues: {
":sendToVal": {
"name" : "First",
"emailAddress" : "first@first.com"
}
}
};
如果您不知道 name的值
和 emailAddress
属性,您可能需要重塑数据以实现用例。
If you don't know the value of name
and emailAddress
attribute, you may need to remodel the data to fulfill your use case.
这篇关于如何在DynamoDB中过滤嵌套数组对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!