DynamoDB中的地图查询列表 [英] Query List of Maps in DynamoDB

查看:61
本文介绍了DynamoDB中的地图查询列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从具有以下格式的dynamodb表中过滤地图列表.

I am trying to filter list of maps from a dynamodb table which is of the following format.

{
   id: "Number",
   users: {
      { userEmail: abc@gmail.com, age:"23" },
      { userEmail: de@gmail.com, age:"41" }
   }
}

我需要使用userEmail作为"abc@gmail.com"来获取用户的数据.目前,我正在使用下面的dynamodb查询.还有其他有效的方法来解决此问题吗?

I need to get the data of the user with userEmail as "abc@gmail.com". Currently I am doing it using the following dynamodb query. Is there any another efficient way to solve this issue ?

 var params = {
        TableName: 'users',
        Key:{
            'id': id
        }
  };
  var docClient = new AWS.DynamoDB.DocumentClient();
  docClient.get(params, function (err, data) {
    if (!err) {
      const users = data.Item.users;
      const user = users.filter(function (user) {
        return user.email == userEmail; 
      });   
      // filtered has the required user in it
  });

推荐答案

如果您的表带有分区键,则可以通过id在dynamo中获得单个项目的唯一方法.因此,您需要一个看起来像这样的表:

The only way you can get a single item in dynamo by id if you have a table with a partition key. So you need to have a table that looks like:

电子邮件(字符串)-分区键

ID (某些类型)-用户ID

Id (some-type) - user id

...其他相关的用户数据

...other relevant user data

不幸的是,由于嵌套字段不能成为分区键,因此您将不得不在此处维护一个单独的表,并且将无法使用DynamoDB(既不是LSI也不是GSI)中的索引.

Unfortunately, since a nested field cannot be a partition key you will have to maintain a separate table here and won't be able to use an index in DynamoDB (neither LSI, nor GSI).

这是NoSQL中复制数据的常见模式,因此其中没有异常.如果您使用的是Java,则可以使用交易库,以确保两个表都同步

It's a common pattern in NoSQL to duplicate data, so there is nothing unusual in it. If you were using Java, you could use transactions library, to ensure that both tables are in sync.

如果您不打算使用Java,则可以阅读 DynamoDB流原始数据库(电子邮件为嵌套字段),并在更新原始表时更新新表(电子邮件为分区键).

If you are not going to use Java you could read DynamoDB stream of the original database (where emails are nested fields) and update the new table (where emails are partition keys) when an original table is updated.

这篇关于DynamoDB中的地图查询列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆