StackOverflowed
StackOverflowed

Reputation: 5975

How to query a value in redis in node.js?

Assuming I have a structure such as:

house{rooms: 2, bathrooms: 1, square-feet: 1000},
house{rooms: 3, bathrooms: 1.5, square-feet: 1200},
house{rooms: 5, bathrooms: 3, square-feet: 1400},

Assuming I want to retrieve all houses where rooms = 3, how would I do that? Currently the key for each object is the zipcode which I need to use to look up and sort. I know in mongoose you can do something like:

var house = House.find({rooms: '3'}); but I can't find the equivalent command using redis.

Upvotes: 0

Views: 944

Answers (1)

Lugg
Lugg

Reputation: 197

Try the ZADD Command to sort the houses by #rooms ZADD is a ordered set that allows indexing by numbers.

ZADD "sorted set name" "sorting value" "value" "key"

so in your example :

ZADD housesByRooms 2 house1
ZADD housesByRooms 3 house2
(and so on you can also add multiple values at once)

http://redis.io/commands/ZADD

later you can get them by ZRANGE

ZRANGE "sorted set name" "from" "to" for example
ZRANGE housesByRooms 2 5
to get houses from 2 to 5 rooms

http://redis.io/commands/zrange

key to redis is to check the commands and test them :)

Upvotes: 1

Related Questions