子阵列上的JSON搜索

问题描述:

{ 
    "name": "testcase #1", 
    "words": [ 
    "the", 
    "quick", 
    "brown", 
    "fox", 
    "jump", 
    "over", 
    "the", 
    "lazy", 
    "dog" 
    ], 
    "values": [ 
    1, 
    1, 
    4, 
    4, 
    1, 
    6, 
    1, 
    3, 
    5 
    ] 
}, 
{ 
    "name": "testcase #2", 
    "words": [ 
    "the", 
    "second", 
    "test", 
    "about", 
    "jump", 
    "over", 
    "the", 
    "lazy", 
    "dog" 
    ], 
    "values": [ 
    3, 
    2, 
    4, 
    5, 
    3, 
    6, 
    4, 
    3, 
    1 
    ] 
} 

我该如何在dynamoDb中制定一个查询来搜索特定“词”所具有的特定值的范围内的所有记录。 上面的示例记录将匹配以下任何一项,并返回整个匹配记录。子阵列上的JSON搜索

"the" < 2 
"the" = 1 
"brown" > 3 

我也可以要求返回所有值小于3的“单词”将返回;

"the", "quick", "second", "dog" 

伊夫搜索周围,但无法找到如何做到这一点明确的文档,而不必扫描可能有重大的性能和成本影响整个表。

+0

你试过了什么? –

+0

其实并不多,我不知道从哪里开始... –

实际上,在帖子提供的上述结构中,不可行来形成过滤表达式。在数据模型

建议更改: -

商店words属性作为map DynamoDB的数据类型。

例子: -

Storing words as map

查询: -

请注意,如果你想使用DynamoDB 查询API,你必须有HASH的关键数据。如果您没有散列密钥数据,则需要使用扫描API或需要GSI(全球二级索引)。

实施例的查询表达式过滤由字的数据计数: -

请注意,我已经使用了散列键值中KeyConditionExpression和其他属性“测试用例1”上FilterExpression

如果您没有散列码,则需要使用扫描API。

var table = "testcase"; 

var params = { 
    TableName : table, 
    KeyConditionExpression : '#name = :hkey', 
    FilterExpression: 'words.the < :wordval1 and words.the = :wordval2 and words.brown > :wordval3', 
    ExpressionAttributeNames : { 
     '#name' : 'name' 
    }, 
    ExpressionAttributeValues : { 
     ':hkey' : 'testcase 1', 
     ':wordval1' : 2, 
     ':wordval2' : 1, 
     ':wordval3' : 3 
    } 
}; 

docClient.query(params, function(err, data) { 
    if (err) { 
     console.error("Unable to read item. Error JSON:", JSON.stringify(err, 
       null, 2)); 
    } else { 
     console.log("GetItem succeeded:", JSON.stringify(data, null, 2)); 
    } 
}); 
+0

谢谢我会研究你的解决方案。实际的整体结构要复杂得多,但这会给我一个很好的起点。一旦我看到它,我可能会回来更多的问题。 –

+0

实际上,在上例中,可能不起作用,因此单词“the”应该与可能不同的数值相关联。在整个结构中,我也有更多的信息与每个在这里没有显示但是存在于大结构中的单词相关联。 –