bernhardh
bernhardh

Reputation: 3309

CouchDB View - Multiple Filter

I have a question to couchdb views / map-reduce.

Lets say, we have a database with hotel-documents like the following:

[{
        _id: "1",
        name: "Hotel A",
        type: "hotel",
        stars: 3,
        flags: ["family-friendly","green-hotel","sport"],
        hotelType: "premium",
        food: ["breakfast","lunch"]
    }, {
        _id: "2",
        name: "Hotel B",
        type: "hotel",
        stars: 5,
        flags: ["family-friendly","pet-friendly"],
        hotelType: "budget",
        food: ["breakfast","lunch","dinner"]
    }]

To find all hotels with 3 stars, the following view will fit:

function(doc) {
    emit([doc.stars, doc.name]);
}

If I use startkey=[3], everything is fine.

But how is it possible to make a view with multiple filters?

For example - all hotels:

Any ideas?

EDIT: I have now decided to use good old mysql. CouchDB was a nice experience for me, but there are tooo much problems if you need more then the data of one document :(

Upvotes: 4

Views: 1895

Answers (3)

Anthony
Anthony

Reputation: 12736

CouchDB views are one-dimensional. And you are looking for multi-dimensional query:

  • x = stars
  • y = flags
  • z = hotelType

Multi-dimensional queries are not supported unfortunately. For example if you need query geographical location by latitude and longitude, than you'll have to use GeoCouch.

Upvotes: 0

pokstad
pokstad

Reputation: 3461

You can emit a key with a group of values:

emit([[doc.stars,doc.hotelType], doc.name]);

The problem is that this only works if you can order your attributes by importance since they will always get reduced in the same order. Kxepal's solution of using different views is probably the best for your situation.

Source: http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views#Grouping

Upvotes: 2

Kxepal
Kxepal

Reputation: 4679

You need to use different views for that. Each view will handle his own domain with own keys. You may create one-view-for-all-data via multiple emit with different key value, but in perspective it will be hard to maintain.

Upvotes: 1

Related Questions