Javier S
Javier S

Reputation: 115

How can I query multiple key criteria?

Using couchdb, with the following json:

{"total_rows":3,"offset":0,"rows":[ {"id":"bc26e5eae7f8c8c3486818e7e7971df0","key":{"user":"[email protected]","pal":["igol ≠ eagle"],"fecha":"10/5/2014"},"value":null},{"id":"cf0dc2e2874776958c59f2f544b5a750","key":{"user":"[email protected]","pal":["kat ≠cat"],"fecha":"10/6/2014"},"value":null},{"id":"df4ec96088ed52096db064f2ebd2310b","key":{"user":"[email protected]","pal":["dok ≠ duck"],"fecha":"10/7/2014"},"value":null}]}

I would like to query for specific user AND specific date:

for example: ?user="[email protected]"&fecha:"10/6/2014"

I also tried: ?user%3Dlili%40def.com%26fecha%3A10%2F6%2F2014

Needless to say, it isn't currently working as I expected (all results are shown, not only the register needed).

my view func is:

function(doc) {

    if (doc.USER){

        emit({user:doc.USER, pal:doc.palabras, fecha:doc.fecha});  

    }
}

Regards.

Upvotes: 1

Views: 2335

Answers (2)

fet
fet

Reputation: 624

I do the same thing as Ant P but I tend to use strings.

function ( doc ) {
  if ( doc.USER ) {
    emit( 'user-' + doc.USER + '-' + doc.fecha, doc );
  }
}

I would also highly recommend emitting null instead of doc as a value.

Remember, you can always emit more than once depending on what kind of queries you need.

For example, if you're looking for all posts by a specific user between two dates, you could do the following view.

function ( doc ) {
  if ( doc.type == "post" ) {
    emit( 'user-' + doc.nombre, null );
    emit( 'fecha-' + doc.fecha, null );
  }
}

Then you would query the view twice _view/posts?key="user-miUsario", and _view/posts?start_key="fecha-1413040000000"&end_key="fecha-1413049452904". Then, once you have all of the ids from both views, you take the intersection and use _all_docs to get your original documents.

You end up making three requests but it saves disk space in the view, the payloads are smaller because you return null, and your code is simpler because you can query the same view multiple ways.

Upvotes: 1

Ant P
Ant P

Reputation: 25231

Remember that CouchDB views are simply key/value lookups that are built at index-time, not query time. At the minute you are emitting a key with no value. If you want to look something up by two values, you'll need to emit a composite key (an array):

function(doc) {
    if (doc.USER) {
        emit([doc.USER, doc.fecha], doc);
    }
}

Then you can look up matching documents by passing the array as the key:

?key=%5B%22lili%40def.com%22%2C%20%2210%2F6%2F2014%22%5D

There are optimisations you can make to this (e.g. emitting a null value and using include_docs to reduce the size of the view) but this should set you off on the right track.

Upvotes: 2

Related Questions