DynamoDB 查询布尔键 [英] DynamoDB query on boolean key

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

问题描述

我是 DynamoDB 的新手(以及一般的 noSQL),我正在努力理解一些概念.特别是给我带来了一些问题,即基于布尔键查询表.

I'm new to DynamoDB (and to noSQL in general) and am struggling a little to get my head round some of the concepts. One thing in particular is giving me some problems, which is around querying a table based on a boolean key.

我意识到我无法在布尔键上创建主索引或二级索引,但我看不出我应该如何理想地索引和查询具有以下结构的表;

I realize that I can't create a primary or secondary index on a boolean key, but I can't see how I should ideally index and query a table with the following structure;

reportId: string (uuid)
reportText: string
isActive: boolean
category: string

我希望能够完成以下搜索:

I would like to be able to complete the following searches:

  1. 直接访问特定报告(reportId 的主哈希索引)
  2. 列出特定类别的报告(在类别)

这些都很简单,但我想执行另外两个查询;

These are both straightforward, but I would like to perform two other queries;

  1. 列出所有标记为 isActive = true 的报告
  2. 列出标记为 isActive 的特定类别的所有报告= 真

我的第一种方法是在 isActive 上创建一个主 hashkey 索引,在 category 上有一个 rangekey,但我只能选择 String, NumberBinary 作为属性类型.

My first approach would be to create a primary hashkey index on isActive, with a rangekey on category, but I'm only able to choose String, Number of Binary as the attribute type.

isActive 存储为字符串(保存为 'true' 而不是布尔值 true)可以解决问题,但是将字符串用于布尔属性会很糟糕.

Storing isActive as a string (saved as 'true' rather than a boolean true) solves the problem, but its horrible using a string for a boolean property.

我错过了什么吗?有没有一种简单的方法可以直接在布尔值上查询表?

Am I missing something? Is there a simple way to query the table directly on a boolean value?

对任何建议表示赞赏.

提前致谢.

推荐答案

我的项目包括这个特定的场景,我遵循了使用 稀疏索引 本地和全球二级索引.这是我对您的示例所做的:

My project includes this particular scenario and I've followed the DynamoDB best practice of using sparse indexes on both Local and Global Secondary Indexes. Here is what I would do with your example:

Table: reportId (string, hash key) || reportText (string) || isActive (string, marked as "x") || category (string)

ActiveReportsIndex (Local Secondary Index): reportID (hash key) || isActive (range key)

ActiveReportsByCategoryIndex (Global Secondary Index): category (hash key) || isActive (range key) || reportId

稀疏索引背后的想法是,只有标记为 isActive: "x" 的报告才会显示在您的索引中,因此它们应该比您的主表需要更少的存储和处理.不要将 isActive 属性设置为布尔类型,该类型将始终存储 truefalse 值,而是在报告时使用x"之类的字符串或您想要的任何其他内容处于活动状态并在报表不活动时完全删除该属性.有意义吗?

The idea behind sparse indexes is that only reports marked as isActive: "x" will show up in your indexes, so they should require less storage and processing than your main table. Instead of making the isActive attribute a boolean type, which will always store a true or false value, use use a string like "x" or anything else you want when the report is active and DELETE the attribute completely when the report is not active. Makes sense?

更新:如果您在查询时需要特定类型的排序(例如按时间顺序),请使用数字(例如 unix 时间戳)而不是x"字符串.

UPDATE: If you want a specific kind of sort when you query (e.g. chronological), use a number (e.g. a unix timestamp) instead of an "x" string.

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

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