简体   繁体   English

DynamoDB 一开始没有返回预期结果

[英]DynamoDB begins with not returning expected results

I'm using NodeJS and DynamoDB.我正在使用 NodeJS 和 DynamoDB。 I'm never used DynamoDB before, and primary a C# developer (where this would simply just be a .Where(x => x... ) call, not sure why Amazon made it any more complicated then that).我以前从未使用过 DynamoDB,主要是 C# 开发人员(这只是一个.Where(x => x... ) 调用,不知道为什么亚马逊让它变得更复杂)。 I'm trying to simply just query the table based on if an id starts with certain characters.我试图简单地根据 id 是否以某些字符开头来查询表。 For example, we have the year as the first 2 characters of the Id field.例如,我们将年份作为Id字段的前 2 个字符。 So something like this: 180192 , so the year is 2018. The 20 part is irrelevant, just wanted to give a human readable example.所以像这样: 180192 ,所以年份是 2018 年20部分无关紧要,只是想给出一个人类可读的例子。 So the Id starts with either 18 or 17 and I simply want to query the db for all rows that Id starts with 18 (for example, could be 17 or whatever).所以 Id 以1817开头,我只想查询数据库以查找 Id 以18开头的所有行(例如,可以是 17 或其他)。 I did look at the documentation and I'm not sure I fully understand it, here's what I have so far that is just returning all results and not the expected results.我确实查看了文档,但我不确定我是否完全理解它,这是我目前所拥有的,只是返回所有结果而不是预期结果。

   let params = {
        TableName: db.table,
        ProjectionExpression: "id,CompetitorName,code",
        KeyConditionExpression: "begins_with(id, :year)",
        ExpressionAttributeValues: {
            ':year': '18'
        }
    return db.docClient.scan(params).promise();

So as you can see, I'm thinking that this would be a begins_with call, where I look for 18 against the Id.正如您所看到的,我认为这将是一个begins_with调用,我在其中根据 Id 查找 18。 But again, this is returning all results (as if I didn't have KeyConditionExpression at all).但同样,这将返回所有结果(就好像我根本没有 KeyConditionExpression 一样)。

Would love to know where I'm wrong here.很想知道我哪里错了。 Thanks!谢谢!

UPDATE更新

So I guess begin_with won't work since it only works on strings and my id is not a string.所以我猜begin_with不会起作用,因为它只适用于字符串,而我的 id 不是字符串。 As per commenters suggestion, I can use BETWEEN , which even that is not working either.根据评论者的建议,我可以使用BETWEEN ,即使这样也不起作用。 I either get back all the results or Query key condition not supported error (if I use .scan , I get back all results, if I use .query I get the error)我要么取回所有结果,要么返回Query key condition not supported的错误(如果我使用.scan ,我会取回所有结果,如果我使用.query ,我会收到错误)

Here is the code I'm trying.这是我正在尝试的代码。

        let params = {
        TableName: db.table,
        ProjectionExpression: "id,CompetitorName,code",
        KeyConditionExpression: "id BETWEEN :start and :end",
        ExpressionAttributeValues: {
            ':start': 18000,
            ':end': 189999
        }
    };

return db.docClient.query(params).promise();

It seems as if there's no actual solution for what I was originally trying to do unfortunately. 不幸的是,似乎对于我最初尝试做的事情没有实际的解决方案。 Which is a huge downfall of DynamoDB. 这是DynamoDB的巨大失败。 There really needs to be some way to do 'where' using the values of columns, like you can in virtually any other language. 确实需要某种方法来使用列的值“在哪里”,就像您几乎可以使用任何其他语言一样。 However, I have to admit, part of the problem was the way that id was structured. 但是,我不得不承认,部分问题是id结构方式。 You shouldn't have to rely on the id to get info out of it. 您不必依靠id来获取信息。 Anyways, I did find another column DateofFirstCapture which using with contains (all the dates are not the same format, it's a mess) and using a year 2018 or 2017 seems to be working. 无论如何,我确实找到了另一列DateofFirstCapture ,该列与contains DateofFirstCapture使用(所有日期都不相同,这是一团糟),并且使用20182017似乎有效。

if you want to fetch data by id, add it as the partition key. 如果要按ID提取数据,请将其添加为分区键。 If you want to get data by part of the string, you can use "begins with" on sort key. 如果要按字符串的一部分获取数据,则可以在排序键上使用“开头为”。

在此处输入图片说明

begins_with (a, substr)— true if the value of attribute a begins with a particular substring. starts_with(a,substr)-如果属性a的值以特定的子字符串开头,则为true。

source: https://docs.amazonaws.cn/en_us/amazondynamodb/latest/developerguide/Query.html 来源: https : //docs.amazonaws.cn/en_us/amazondynamodb/latest/developerguide/Query.html

begins_with and between can only be used on sort keys. begins_withbetween只能用于排序键。 For query you must always supply partition key.对于query ,您必须始终提供分区键。

So if you change your design to have unique partition key (or unique combo of partition/sort keys) and strings like 180192 as sort key you will be able to query begins_with(sortkey, ...) .因此,如果您将设计更改为具有唯一的分区键(或分区/排序键的唯一组合)和像180192这样的字符串作为排序键,您将能够查询begins_with(sortkey, ...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM