newJin
newJin

Reputation: 1

How can I sort the query results in Dynamoose based on the rangeKey of a specific index, instead of the default rangeKey?

When querying DynamoDB using the Dynamoose library, the results are by default sorted by the rangeKey defined in the model schema. However, when I use a different index, the results should be sorted by the rangeKey defined in that index, but they are still sorted by the default rangeKey. What would be the solution to this problem?

const dynamoose = require("dynamoose");

const schema = new dynamoose.Schema(
  {
    a: {
      type: Number,
      required: true,
      hashKey: true,
    },
    b: {
      type: String,
      required: true,
      rangeKey: true,  // default sort
    },
    c: {
      type: String,
      required: true,
      index: {
        global: false,
        name: "c-index",
        rangeKey: "c",  // I would like to receive the results sorted by this column.
        project: true,
        throughput: "ON_DEMAND",
      },
    }
  },
);
const result = await sometingModel.query("a")
.eq(a)
.using("c-index")
.where("c")
.ge(c)
.all()
.exec();

I want to receive values sorted based on column c, but the results are shown sorted based on column b. How can I get the values sorted according to column c?

Upvotes: 0

Views: 192

Answers (1)

Leeroy Hannigan
Leeroy Hannigan

Reputation: 19683

DynamoDB always returns the results based on the sort key of the target table/index.

While I can't speak for Dynamoose, here is my suggestions:

  1. Inspect the http request sent over the wire to DynamoDB, ensure it contains the index name.
  2. Inspect the response as it's sent over the wire, check for ordering before deserialisation.
  3. If the issue is with Dynamoose then raise an issue in their Guy
  4. If it seems like there's an issue with DynamoDB then come back here to me.

Upvotes: 0

Related Questions