2017-05-03 203 views
0

我的DynamoDB中有以下數據。困惑於查詢DynamoDB

enter image description here

,我試圖達到如下的結果。 掃描整個表格並獲取management is NULL and Location is Midwest所在的行。

我最初嘗試以下查詢以匹配Null

var scanningParameters = { 
    TableName: 'LOB', 
    FilterExpression: "#mgmt contains NULL", 
    ExpressionAttributeNames: { 
     "#mgmt": "Management", 
    } 
}; 

docClient.scan(scanningParameters, onScan); 
function onScan(err, data) { 
    if (err) { 
     console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2)); 
    } else { 
     // print all the movies 
     console.log("Scan succeeded."); 
     data.Items.forEach(function (data) { 
      console.log(
       data.lineofbusiness + " name : ", 
       data.name); 
     }); 

     if (typeof data.LastEvaluatedKey != "undefined") { 
      console.log("Scanning for more..."); 
      scanningParameters.ExclusiveStartKey = data.LastEvaluatedKey; 
      docClient.scan(scanningParameters, onScan); 
     } 
    } 
} 

我得到的異常,因爲

{ 
    "message": "Invalid FilterExpression: Syntax error; token: \"contains\", near: \"#mgmtcontains NULL\"", 
    "code": "ValidationException", 
    "time": "2017-05-03T13:21:11.611Z", 
    "requestId": "0T0GU59HRJ24P96D42H9QNC97RVV4KQNSO5AEMVJF66Q9ASUAAJG", 
    "statusCode": 400, 
    "retryable": false, 
    "retryDelay": 13.73953651636839 
} 

請讓我知道我要去哪裏錯了,我怎麼能解決這個問題。

+0

我假設你有管理的價值爲NULL屬性 – notionquest

+0

是在管理該值爲NULL – user3872094

+0

即使掃描是足夠好,我可以得到休息 – user3872094

回答

0

以下是「NULL」值(即具有NULL作爲數據的String屬性)的掃描項目。

我假設Management屬性是包含字符串值「NULL」的String數據類型。

代碼: -

var AWS = require("aws-sdk"); 
var creds = new AWS.Credentials('akid', 'secret', 'session'); 

AWS.config.update({ 
    region: "us-west-2", 
    endpoint: "http://localhost:8000", 
    credentials : creds 
}); 

var docClient = new AWS.DynamoDB.DocumentClient(); 

var params = { 
    TableName: "lob", 
    FilterExpression: "#mgmt = :mgmtVal", 
    ExpressionAttributeNames: { 
     "#mgmt": "Management", 
    }, 
    ExpressionAttributeValues : { 
     ":mgmtVal" : "NULL" 
    } 
}; 

docClient.scan(params, onScan); 
var count = 0; 

function onScan(err, data) { 
    if (err) { 
     console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2)); 
    } else { 
     console.log("Scan succeeded."); 
     data.Items.forEach(function(itemData) { 
      console.log("Item :", ++count,JSON.stringify(itemData)); 
     }); 

     if (typeof data.LastEvaluatedKey != "undefined") { 
      params.ExclusiveStartKey = data.LastEvaluatedKey; 
      docClient.scan(params, onScan); 
     } 
    } 
}