2015-12-18 10 views
10

Sto tentando di eseguire una query su una tabella DynamoDB per trovare tutti gli elementi in cui l'attributo email non è impostato. Un indice secondario globale chiamato EmailPasswordIndex esiste nella tabella che include il campo email.Come si esegue una query per un attributo inesistente (null) in DynamoDB

var params = { 
    "TableName": "Accounts", 
    "IndexName": "EmailPasswordIndex", 
    "KeyConditionExpression": "email = NULL", 
}; 

dynamodb.query(params, function(err, data) { 
    if (err) 
     console.log(JSON.stringify(err, null, 2)); 
    else 
     console.log(JSON.stringify(data, null, 2)); 
}); 

Risultato: definizione

{ 
    "message": "Invalid KeyConditionExpression: Attribute name is a reserved keyword; reserved keyword: NULL", 
    "code": "ValidationException", 
    "time": "2015-12-18T05:33:00.356Z", 
    "statusCode": 400, 
    "retryable": false 
} 

Tabella:

var params = { 
    "TableName": "Accounts", 
    "KeySchema": [ 
     { "AttributeName": "id", KeyType: "HASH" }, // Randomly generated UUID 
    ], 
    "AttributeDefinitions": [ 
     { "AttributeName": "id", AttributeType: "S" }, 
     { "AttributeName": "email", AttributeType: "S" }, // User e-mail. 
     { "AttributeName": "password", AttributeType: "S" }, // Hashed password. 
    ], 
    "GlobalSecondaryIndexes": [ 
     { 
      "IndexName": "EmailPasswordIndex", 
      "ProvisionedThroughput": { 
       "ReadCapacityUnits": 1, 
       "WriteCapacityUnits": 1 
      }, 
      "KeySchema": [ 
       { "AttributeName": "email", KeyType: "HASH" }, 
       { "AttributeName": "password", KeyType: "RANGE" }, 
      ], 
      "Projection": { "ProjectionType": "ALL" } 
     }, 
    ], 
    ProvisionedThroughput: {  
     ReadCapacityUnits: 1, 
     WriteCapacityUnits: 1 
    } 
}; 

dynamodb.createTable(params, function(err, data) { 
    if (err) 
     console.log(JSON.stringify(err, null, 2)); 
    else 
     console.log(JSON.stringify(data, null, 2)); 
}); 
+0

Puoi fornire la tabella e le definizioni di attributo indice? – mkobit

+0

@mkobit Aggiunto, grazie. –

risposta

15

indici secondari globale di DynamoDB consentono per gli indici di essere sparse. Ciò significa che se si dispone di un GSI la cui chiave hash o intervallo per un articolo non è definita, quell'elemento non sarà semplicemente incluso nel GSI. Questo è utile in una serie di casi d'uso in quanto consente di identificare direttamente i record che contengono determinati campi. Tuttavia, questo approccio non funzionerà se stai cercando la mancanza di un campo.

Per ottenere che tutti gli articoli con un campo non impostato siano la soluzione migliore potrebbe ricorrere a una scansione con un filtro. Questa operazione sarà molto costoso, ma che sarebbe stato semplice codice di guardare qualcosa di simile al seguente:

var params = { 
    TableName: "Accounts", 
    FilterExpression: "attribute_not_exists(email)" 
}; 

dynamodb.scan(params, { 
    if (err) 
     console.log(JSON.stringify(err, null, 2)); 
    else 
     console.log(JSON.stringify(data, null, 2)); 
}); 
0

@jaredHatfield è corretta se il campo non esiste, ma che non funziona se il archiviato è nullo. NULL è una parola chiave e non può essere utilizzata direttamente. Ma puoi usarlo con ExpressionAttributeValues.

const params = { 
    TableName: "Accounts", 
    FilterExpression: "attribute_not_exists(email) or email = :null", 
    ExpressionAttributeValues: { 
     ':null': null 
    } 
} 

dynamodb.scan(params, (err, data) => { 
    if (err) 
     console.log(JSON.stringify(err, null, 2)); 
    else 
     console.log(JSON.stringify(data, null, 2)); 
}) 
Problemi correlati