Home > Software engineering >  how to find the whole array using the array's element in dynamodb?
how to find the whole array using the array's element in dynamodb?

Time:12-23

In DB there is an attribute (name "user_ids") in form of an array that contains user-id [a, b, c, d...]. I want to search that whole array using a single user-id.

CodePudding user response:

Unfortunately, in this case, you have to scan the whole table. DDB is not optimized for this type of operation.

CodePudding user response:


var params = {
    TableName: 'my-table-name',
    FilterExpression: "#users = :id",
    ExpressionAttributeNames: {
        "#users": "users"
    },
    ExpressionAttributeValues: {
        ":id": ["KwV-yfctBcwCHIw="] // user-id
    }
};


dynamo.scan(params, (err, data) => {
    if (err) console.error({ err });

    console.log(data); // output -> { 'room-id': 'group-2', link: 'asdf', users: ["KwV-yfctBcwCHIw=", "Kqc-wfctacwCsww=", "lqw-yfftBcwqwIw="] },
})

CodePudding user response:

yes you can do this using filterexpression, it's depends how you are storing the data . either it's "Document Types" (List/Map) or it's Sets,just give a try to filterexpression

you could refer initial aws documentation or there refer ton of example available online.

please refer this link... have some sample code.

  • Related