Amazon web services 带“或”条件的Amazon DynamoDB
我刚从amazon dynamoDB开始,我必须创建一个类似这样的无sql数据库结构Amazon web services 带“或”条件的Amazon DynamoDB,amazon-web-services,amazon-dynamodb,Amazon Web Services,Amazon Dynamodb,我刚从amazon dynamoDB开始,我必须创建一个类似这样的无sql数据库结构 -posts -postId1 -tags 1:A 2:B 3:C -text:Hello -postId2 -tags 1:B 2:D -text:How are you? -postId3
-posts
-postId1
-tags
1:A
2:B
3:C
-text:Hello
-postId2
-tags
1:B
2:D
-text:How are you?
-postId3
-tags
1:A
2:C
-text:Hello World
现在,我想检索那些带有标记B或D的帖子ID的文本,实现这一点最简单的方法是什么?如评论中所述,如果您将标记属性维护为DynamoDB list数据类型,您可以使用CONTAINS with或operator来检查筛选带有标记B或D的帖子 扫描API的示例参数:- 完整代码:- 下面的代码使用本地DynamoDB
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: "post",
FilterExpression: "contains (tags, :tag1) OR contains (tags, :tag2)",
ExpressionAttributeValues: {
":tag1": 'B',
":tag2": 'D'
}
};
console.log("Scanning Post table.");
docClient.scan(params, onScan);
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 (printItem) {
console.log("Item :", JSON.stringify(printItem));
});
if (typeof data.LastEvaluatedKey != "undefined") {
console.log("Scanning for more...");
params.ExclusiveStartKey = data.LastEvaluatedKey;
docClient.scan(params, onScan);
}
}
}
看起来您需要执行操作。是否可以对标记列表中的多个项使用或条件?可以这样做,也可以只使用筛选器表达式中的关键字与枚举的值列表进行比较。我不确定你的标签是地图还是列表。它可以是任何东西,在这个结构中,我像地图一样显示它,但如果我使用列表,那就好了。类似的东西
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: "post",
FilterExpression: "contains (tags, :tag1) OR contains (tags, :tag2)",
ExpressionAttributeValues: {
":tag1": 'B',
":tag2": 'D'
}
};
console.log("Scanning Post table.");
docClient.scan(params, onScan);
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 (printItem) {
console.log("Item :", JSON.stringify(printItem));
});
if (typeof data.LastEvaluatedKey != "undefined") {
console.log("Scanning for more...");
params.ExclusiveStartKey = data.LastEvaluatedKey;
docClient.scan(params, onScan);
}
}
}