【问题标题】:Dynamodb query on date attribute for greater and less than operatorDynamodb 查询大于和小于运算符的日期属性
【发布时间】:2018-03-29 15:21:35
【问题描述】:

我在 Dynamodb 中创建了具有以下详细信息的表

class OrderStatusIndex(GlobalSecondaryIndex):
    class Meta:
        index_name = 'OrderStatusIndex'
        read_capacity_units = 2
        write_capacity_units = 1
        projection = AllProjection()
    order_status = NumberAttribute(hash_key=True)
    created_on = UnicodeAttribute(range_key=True)


class Order(Model):

    class Meta:
        table_name = 'order'
        host = 'http://localhost:8000'
        read_capacity_units = 2
        write_capacity_units = 1

    order_id = NumberAttribute(hash_key=True)
    order_status = NumberAttribute(hash_key=True)
    created_on = UnicodeAttribute(range_key=True)
    order_status_index = OrderStatusIndex()

现在我想根据特定日期的订单状态进行查询 如果我做 mysql 查询,这将如下所示

select *from order where order_status = 0 and created_on >= start_date and created_on <=end_date

有什么方法可以在 Dynamodb 中使用 pynamodb 实现相同的目标

【问题讨论】:

    标签: python amazon-dynamodb


    【解决方案1】:

    试试这个:

    for order_item in Order.scan(Order.order_status == 0 & (Order.created_on >= start_date) & (Order.created_on <= end_date)):
        print(order_item)
    

    pynamodb API 文档的链接:

    http://pynamodb.readthedocs.io/en/latest/api.html#pynamodb.models.Model.scan

    您可以参考以下链接了解如何在pynamodb 查询中使用条件表达式。

    https://pynamodb.readthedocs.io/en/latest/batch.html#query-filters

    https://pynamodb.readthedocs.io/en/latest/conditional.html#condition-expressions

    希望这会有所帮助。

    【讨论】:

    • 这是非常昂贵的,而且不是一个好方法,因为它会扫描整个表。您应该查看索引。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 1970-01-01
    • 2013-02-16
    相关资源
    最近更新 更多