HEXISTS:检查字段是否存在

HEXISTS 命令可用于检查用户给定的字段是否存在于散列当中:

HEXISTS hash field

如果散列包含了给定的字段,那么命令返回 1,否则命令返回 0。

例如,以下代码就展示了如何使用 HEXISTS 命令检查 article::10086 散列是否包含某些字段:

redis> HEXISTS article::10086 author
(integer) 1 -- 包含该字段
redis> HEXISTS article::10086 content
(integer) 1
redis> HEXISTS article::10086 last_updated_at
(integer) 0 -- 不包含该字段

从 HEXISTS 命令的执行结果可以看出,article::10086 散列包含了 author 字段和 content 字段,但却没有包含 last_updated_at 字段。

如果用户给定的散列并不存在,那么 HEXISTS 命令对于这个散列所有字段的检查结果都是不存在:

redis> HEXISTS not-exists-hash not-exists-field
(integer) 0
redis> HEXISTS not-exists-hash another-not-exists-field
(integer) 0

其他信息

  • 复杂度:O(1)。

  • 版本要求:HEXISTS 命令从 Redis 2.0.0 版本开始可用。