-1

I'm trying to query some data between 2 dates from dynamoDb using dynamoose with nestjs. I have a history table with this schema:

const HistorySchema = new Schema(
  {
    messageId: {
      type: String,
      hashKey: true,
    },

    sendAt: {
      type: Date,
      rangeKey: true,
      index: {
        name: 'sendAtIndex',
        global: true,
      },
    },
  }
);

And I'm trying to run this query


const condition = new Condition()
      .filter('sendAt')
      .between(1, new Date().getTime());
const query = await HistoryModel
      .query(condition)
      .using('sendAtIndex')
      .exec();
    console.log(query);

Debugging the query gave me this

{
  "ExpressionAttributeNames": {
      "#qha": "sendAt"
  },
  "ExpressionAttributeValues": {
      ":qhv_1": {
          "N": "1"
      },
      ":qhv_2": {
          "N": "1690365822635"
      }
  },
  "TableName": "histories",
  "IndexName": "sendAtIndex",
  "KeyConditionExpression": "#qha BETWEEN :qhv_1 AND :qhv_2"
}

And all I'm getting is this error message: Query key condition not supported

Is between condition not supported with Date type? If so which type of data should I use?

NganCun
  • 155
  • 1
  • 4
  • 13

1 Answers1

2

You must also specify the partition key for a Query request.

You must provide the name of the partition key attribute and a single value for that attribute. Query returns all items with that partition key value. Optionally, you can provide a sort key attribute and use a comparison operator to refine the search results.

const condition = new Condition()
      .filter('sendAt')
      .between(1, new Date().getTime());
const query = await HistoryModel
      .query("gsipk").eq("gsipkvalue")
      .where(condition)
      .using('sendAtIndex')
      .exec();
    console.log(query);
Leeroy Hannigan
  • 11,409
  • 3
  • 14
  • 31
  • So do I always have to query in 1 partition only? Is there a way I can query the data in all partitions? – NganCun Jul 26 '23 at 11:43
  • 1
    Not without creating an index with a static value, which is the same across all your items. – Leeroy Hannigan Jul 26 '23 at 12:01
  • Will it be too costly to do that? How much space would an index like that take up? And at this point is it even worth to use a noSQL database then? Or should I just stick with SQL – NganCun Jul 26 '23 at 12:14
  • 1
    It depends on your reasoning for using NoSQL in the first place. The index will be the same size as your base table, if you choose to project all attributes. You can decide to only project a small amount if that works for your use-case. – Leeroy Hannigan Jul 26 '23 at 12:27