Assume I have multiple hash lists as below:
HMSET myhash1 type Car company BMW
HMSET myhash2 type Car company Benz
HMSET myhash3 type Car company BMW
HMSET myhash4 type Car company Honda
HMSET myhash5 type Car company BMW
HMSET myhash6 type Car company Toyota
HMSET myhash7 type Car company Benz
I want to count how many hash list I have with company = BMW
which is 3
2
Answers
You have to build some form of secondary index to accomplish this efficently.
Use a Set
You can use a set, and create a set for each company, and add each key to it.
Then when you want to query it, you would just use SCARD, so if you wanted to know how many BMWs there were you’d just run
With Redis Stack
Redis Stack has native secondary indexing capabilities which you can leverage here this is much easier to maintain (and can actually work across shards in a scaled out environment if need be). You’d just need to create the secondary index
Then you’d just need to query them (if you don’t care to get the actual cars matching your query back just pass in
LIMIT 0 0
The thing with non-relational databases is that you have to make relations by hand if needed. So here you are "obliged" to have another key holding this information.