0

I want to do a search into my DB looking for an element that is inside a Map.
The element that I've been looking for is reference

DB SCHEMA EXAMPLE

{
    id: "someid",
    payment_method:{
        reference:"some Reference",
        typeOf : "Card"
    }
    amount:500
} 

I want to look if the reference that the client sends to me is actually in my DB.

This what I tried:

function searchReference(event, callback){
    
    console.log(event.folio);
    var params = {
        TableName: process.env.TABLE_REFERENCES,
        FilterExpression: "#idRef = :data",
         ExpressionAttributeNames: {
            "#idRef": "payment_method.reference",
        },
        ExpressionAttributeValues: {
             ":data": event.folio
        }
    };
    
    docClient.scan(params, function(err, data) {
        if (err) {
            console.log("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
            var responseD = buildCallBack(403,"ReferenceError",null);
            callback(err, responseD);
        } else {
            
            console.log("data");
            console.log(data);
            
            if (isEmpty(data)) {
                var responseD = buildCallBack(403,"Reference not found",null);
                callback(null, responseD);
            } else { 
                console.log(data);
                doValidation(callback, event, data);
            }

        }
    });
}

But when the scan is done my data is empty, I'm sure that the reference that I'm trying to search is in the DB, but i really don't know what to change in my code.

This is what i'm receiving

{ Items: [], Count: 0, ScannedCount: 2 }
0

2 Answers 2

1

DynamoDB interprets a dot in an expression attribute name as a character within the attribute's name (as opposed to a separator between levels of a hierarchical attribute's name). So, you need to define an expression attribute name for each element in the document path.

For example, your ExpressionAttributeNames would be:

"#pm":"payment_method"
"#rf":"reference"

And your FilterExpression would be:

"#pm.#ref=:data"

This is not very intuitive, but can be found in the documentation on Nested Attributes.

Sign up to request clarification or add additional context in comments.

Comments

1

realize that Scan() is very costly and slow...as the entire table is read for every scan. Thus it is something you want to stay away from for any "normal" use cases. Personally I stay away from it for everything but ultra-rare use cases.

Even then, understand what that it can easily use up all your provisioned RCU, leaving other calls to have to be retried (usually automatically by the SDK).

Since you have the key, a GetItem() would be a better choice.

You could use the filterexpression as document in jarmod's answer or simply check client side. Personally, I'd check client side that way I'd know if the record was there and that the ref# didn't match versus the record not being there. Assuming that means something to your use case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.