[英]How to perform DynamoDB scan using filterExpression in swift
我有一个表,将有关组的信息(GroupID,成员,创建者,LastAccessed,GroupName等)存储为单独的行。 每个组都有一个唯一的标识符(GroupID)作为其哈希值(主键)。 它们还具有称为GroupName的属性。 我有一个搜索框,用户在其中输入部分组名称。 我想在表上执行扫描,并返回以用户输入开头的所有组。 这是我到目前为止所拥有的..
func searchForGroupsWithName(groupName: String) {
self.queryInProgress = true
let cond = AWSDynamoDBCondition()
let v1 = AWSDynamoDBAttributeValue();
v1.S = groupName
cond.comparisonOperator = AWSDynamoDBComparisonOperator.BeginsWith
cond.attributeValueList = [ v1 ]
let exp = AWSDynamoDBScanExpression()
//I only want to return the GroupName and GroupID.
//I think this should be ["GroupID", "GroupName"], but it requires a string
exp.projectionExpression = ??????????
//I am not sure how to incorporate cond with this.
exp.filterExpression = ??????????
dynamoDBObjectMapper.scan(GroupTableRow.self, expression: exp).continueWithBlock({ (task:AWSTask!) -> AnyObject! in
if task.result != nil {
let paginatedOutput = task.result as! AWSDynamoDBPaginatedOutput
for item in paginatedOutput.items as! [GroupTableRow] {
self.searchBarResults.append(item)
}
if ((task.error) != nil) {
print("Error: \(task.error)")
}
self.queryInProgress = false
return nil
}
self.queryInProgress = false
return nil
})
}
在从WestonE获得帮助之后,我修正了我的代码并提出了一个可行的示例。 我在下面将其粘贴给其他需要类似内容的人
func searchForGroupsWithName(groupName: String) {
self.queryInProgress = true
let lowercaseGroupName = groupName.lowercaseString
let scanExpression = AWSDynamoDBScanExpression()
//Specify we want to only return groups whose name contains our search
scanExpression.filterExpression = "contains(LowercaseName, :LowercaseGroupName)"
//Create a scan expression to state what attributes we want to return
scanExpression.projectionExpression = "GroupID, GroupName, LowercaseName"
//Define our variable in the filter expression as our lowercased user input
scanExpression.expressionAttributeValues = [":LowercaseGroupName" : lowercaseGroupName]
dynamoDBObjectMapper.scan(GroupTableRow.self, expression: scanExpression).continueWithBlock({ (task:AWSTask!) -> AnyObject! in
if task.result != nil {
let paginatedOutput = task.result as! AWSDynamoDBPaginatedOutput
//use the results
for item in paginatedOutput.items as! [GroupTableRow] {
}
if ((task.error) != nil) {
print("Error: \(task.error)")
}
self.queryInProgress = false
return nil
}
self.queryInProgress = false
return nil
})
}
projectionExpression应该是单个逗号分隔的字符串[“ GroupID”,“ GroupName”] =>“ GroupID,GroupName”
filterExpression也是一个字符串,其文档是http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/QueryAndScan.html#FilteringResults 。 在您的情况下,我认为表达式应为“ begins_with(groupName,BeginningCharacters)”,但您可能需要对此进行试验。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.