Skip to main content

Usage

Returns all values in the hash stored at key.
await redis.hvals(key);

Parameters

key
string
required
The key of the hash

Response

values
TData[]
An array of values in the hash. Returns an empty array if the key does not exist.Values are automatically parsed from JSON when possible.

Examples

Get all values

import { Redis } from '@upstash/redis';

const redis = new Redis({
  url: process.env.UPSTASH_REDIS_REST_URL!,
  token: process.env.UPSTASH_REDIS_REST_TOKEN!,
});

// Set hash fields
await redis.hset('user:1000', {
  name: 'Alice',
  email: 'alice@example.com',
  age: 30,
  city: 'New York'
});

// Get all values
const values = await redis.hvals('user:1000');
console.log(values); // ['Alice', 'alice@example.com', 30, 'New York']

Working with typed data

interface UserValue {
  name?: string;
  email?: string;
  age?: number;
  city?: string;
}

type UserValues = Array<string | number>;

const values = await redis.hvals<UserValues>('user:1000');
console.log(values);

Process all values

// Get all product prices
const prices = await redis.hvals<number[]>('product:prices');

// Calculate average
const average = prices.reduce((sum, price) => sum + price, 0) / prices.length;
console.log(`Average price: $${average.toFixed(2)}`);

// Find min and max
const min = Math.min(...prices);
const max = Math.max(...prices);
console.log(`Price range: $${min} - $${max}`);

Non-existent key

const values = await redis.hvals('user:9999');
console.log(values); // []

Count occurrences

// Get all status values
const statuses = await redis.hvals<string[]>('order:statuses');

// Count each status
const statusCounts = statuses.reduce((acc, status) => {
  acc[status] = (acc[status] || 0) + 1;
  return acc;
}, {} as Record<string, number>);

console.log(statusCounts);
// { pending: 5, shipped: 3, delivered: 2 }

Filter values

// Get all ages
const ages = await redis.hvals<number[]>('users:ages');

// Filter adults
const adults = ages.filter(age => age >= 18);
console.log(`Adults: ${adults.length}`);

Check for duplicates

const emails = await redis.hvals<string[]>('users:emails');
const uniqueEmails = new Set(emails);

if (emails.length !== uniqueEmails.size) {
  console.log('Duplicate emails found!');
}

Sum numeric values

// Get all point values
const points = await redis.hvals<number[]>('user:1000:scores');

// Calculate total
const total = points.reduce((sum, points) => sum + points, 0);
console.log(`Total points: ${total}`);

Validate all values

const urls = await redis.hvals<string[]>('config:endpoints');

// Check if all URLs are valid
const allValid = urls.every(url => {
  try {
    new URL(url);
    return true;
  } catch {
    return false;
  }
});

console.log(`All URLs valid: ${allValid}`);

Export values list

// Get all feature flags
const features = await redis.hvals<boolean[]>('app:features');

const enabledCount = features.filter(Boolean).length;
console.log(`${enabledCount} of ${features.length} features enabled`);

Notes

  • The order of values in the returned array is not guaranteed
  • The order corresponds to the order of fields returned by HKEYS
  • Returns an empty array for non-existent keys
  • Use HGETALL if you need both field names and values
  • For very large hashes, consider using HSCAN to iterate incrementally

See Also

  • HKEYS - Get all field names in a hash
  • HGETALL - Get all fields and values in a hash
  • HGET - Get a specific hash field value