Dynamodb scan() 使用 FilterExpression [英] Dynamodb scan() using FilterExpression

查看:27
本文介绍了Dynamodb scan() 使用 FilterExpression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一次在 Stack 上发帖,对使用 Python 和使用 DynamoDB 编程还很陌生,但我只是尝试在我的表上运行扫描,返回基于两个预定义属性的结果.

---这是我的 Python 代码片段---

shift = "3rd"日期 = "2017-06-21"如果 shift != "":响应 = table.scan(FilterExpression=Attr("Date").eq(date) 和 Attr("Shift").eq(shift))

我的 DynamoDB 有 4 个字段.

  1. 身份证
  2. 日期
  3. Shift
  4. 安全

现在对于这个问题,在运行时我得到了两个表条目,而我应该只获取第一个条目......根据我的扫描标准,没有安全问题".

---这是我的DynamoDB返回结果---

<预><代码>[{"Shift": "第三","Safety": "没有安全问题","日期": "2017-06-21",身份证":2"},{"Shift": "第三",安全":割手指","日期": "2017-06-22",身份证":4"}]

退回的物品:2

我相信通过应用带有逻辑and"的 FilterExpression 指定扫描操作正在寻找满足两个条件的条目,因为我使用了and".

这可能是因为在两个条目中都找到了 'shift' 属性3rd"?我如何确保它返回基于满足两个条件的条目,而不仅仅是给我来自一种属性类型的结果?

我觉得这很简单,但我查看了以下可用文档:http://boto3.readthedocs.io/en/latest/reference/services/dynamodb.html#DynamoDB.Table.scan 并且我仍然遇到问题.任何帮助将不胜感激!

附言我尽量使帖子简单易懂(不包括我的所有程序代码)但是,如果需要其他信息,我可以提供!

解决方案

这是因为您在表达式中使用了 Python 的 and 关键字,而不是 & 运算符.

如果ab都被认为是Truea和b返回后者,b:

<预><代码>>>>2 和 33

如果其中任何一个是False,或者两者都是,则返回第一个False对象:

<预><代码>>>>0 和 30>>>0 和 ''0>>>

一般规则是,and 返回第一个允许它决定整个表达式真实性的对象.

Python 对象在布尔上下文中始终被视为 True.所以,你的表情:

Attr("Date").eq(date) 和 Attr("Shift").eq(shift)

将评估为最后一个 True 对象,即:

Attr("Shift").eq(shift)

这就解释了为什么你只过滤了班次.

您需要使用 & 运算符.它通常表示 Python 中整数之间的按位和",它被重新定义为 Attr 对象以表示您想要的内容:两个条件".

所以你必须使用按位与":

FilterExpression=Attr("Date").eq(date) &Attr("Shift").eq(shift)

根据文档

<块引用>

您还可以使用逻辑将条件链接在一起运营商: &(和), |(或)和~(不是).

First post here on Stack and fairly new to programming with Python and using DynamoDB, but I'm simply trying to run a scan on my table that returns results based on two pre-defined attributes.

---Here is my Python code snippet---

shift = "3rd"
date = "2017-06-21"

if shift != "":
    response = table.scan(
        FilterExpression=Attr("Date").eq(date) and Attr("Shift").eq(shift)
    )

My DynamoDB has 4 fields.

  1. ID
  2. Date
  3. Shift
  4. Safety

Now for the issue, upon running I'm getting two table entries returned when I should only be getting the first entry... the one with "No safety issues" based on my scan criteria.

---Here is my DynamoDB return results---

[
  {
    "Shift": "3rd",  
    "Safety": "No safety issues",  
    "Date": "2017-06-21",
    "ID": "2"
  }, 
  {
    "Shift": "3rd", 
    "Safety": "Cut Finger", 
    "Date": "2017-06-22", 
    "ID": "4"
  }
]

Items Returned: 2

I believe that by applying the FilterExpression with the logical 'and' specified that the scan operation is looking for entries that meet BOTH criteria since I used 'and'.

Could this be because the 'shift' attribute "3rd" is found in both entries? How do I ensure it returns entries based on BOTH criteria being meet and not just giving me results from one attribute type?

I have a feeling this is simple but I've looked at the available documentation at: http://boto3.readthedocs.io/en/latest/reference/services/dynamodb.html#DynamoDB.Table.scan and am still having trouble. Any help would be greatly appreciated!

P.S. I tried to keep the post simple and easy to understand (not including all my program code) however, if additional information is needed I can provide it!

解决方案

This is because you used Python's and keyword in your expression, instead of the & operator.

If a and b are both considered True, a and b returns the latter, b:

>>> 2 and 3
3

If any of them is False, or if both of them are, the first False object is returned:

>>> 0 and 3
0
>>> 0 and ''
0
>>> 

The general rule is, and returns the first object that allows it to decide the truthiness of the whole expression.

Python objects are always considered True in boolean context. So, your expression:

Attr("Date").eq(date) and Attr("Shift").eq(shift)

will evaluate as the last True object, that is:

Attr("Shift").eq(shift)

which explains why you only filtered on the shift.

You need to use the & operator. It usually means "bitwise and" between integers in Python, it is redefined for Attr objects to mean what you want: "both conditions".

So you must use the "bitwise and":

FilterExpression=Attr("Date").eq(date) & Attr("Shift").eq(shift)

According to the documentation,

You are also able to chain conditions together using the logical operators: & (and), | (or), and ~ (not).

这篇关于Dynamodb scan() 使用 FilterExpression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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