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

查看:74
本文介绍了布尔键上的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 realise that I can't created 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的特定类别的所有报告= true

我的第一种方法是在 isActive 上创建主哈希键索引,并在 category 上创建rangekey,但是我只能选择 String Boolean Number 作为键类型.

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 Boolean as the key type.

isActive 存储为字符串(保存为'true'而非boolean true)可以解决此问题,但是使用字符串作为boolean属性却很糟糕.

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?

任何建议都应得到赞赏.

Any advice duly appreciated.

提前谢谢.

推荐答案

我的项目包括此特定方案,并且我遵循了DynamoDB最佳实践,即使用

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属性设置为布尔类型(始终存储 true false 值),而是在报告时使用"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天全站免登陆