0

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.

asked Mar 8, 2015 at 8:12
2
  • You don't, unless you create another structure in redis keyed on # of rooms. There isn't any indexing in redis except what you build. Commented Mar 8, 2015 at 8:15
  • Thanks @hobbs. Is there any inmemory cache object that would let me do the above? I guess if I want to do search by # of rooms, then I'd have to create a separate object that is basically a clone of the data of the current collection? Commented Mar 8, 2015 at 8:17

1 Answer 1

1

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 :)

answered Mar 8, 2015 at 22:58
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.