You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Ryan Richins <ri...@mac.com> on 2009/09/08 20:24:56 UTC

How to query multi item key

Hi,

I have been trying to figure out the right way to query a multi item  
key but have not had any luck.

Given the following set of data:

{"key":["27.0.0.1","2009/08/12 18:37:06 +0000"],"value":null},
{"key":["12.0.0.1","2009/08/13 02:29:57 +0000"],"value":null},
{"key":["17.0.0.1","2009/08/14 21:25:27 +0000"],"value":null},
{"key":["31.0.0.1","2009/08/15 02:54:03 +0000"],"value":null},
{"key":["11.0.0.1","2009/08/16 08:35:15 +0000"],"value":null},
{"key":["12.0.0.1","2009/08/17 19:21:59 +0000"],"value":null}

If I want all of the IP's in a given date range (2009/08/13 -  
2009/08/16) what is the right way to construct the query?  The hang-up  
is trying to figure out the right value for the first item in the key  
to act as a '*'
Below are a few of the following I have tried but none of which work.
http://localhost:5984/foo/_design/test/_view/by_ip_and_created_at?startkey= 
[null,"2009/08/13"]&endkey=[null,"2009/08/16"]
http://localhost:5984/foo/_design/test/_view/by_ip_and_created_at?startkey= 
[null,"2009/08/13"]&endkey=[{},"2009/08/16"]
http://localhost:5984/foo/_design/test/_view/by_ip_and_created_at?startkey= 
[{},"2009/08/13"]&endkey=[{},"2009/08/16"]
http://localhost:5984/foo/_design/test/_view/by_ip_and_created_at?startkey= 
["","2009/08/13"]&endkey=["","2009/08/16"]

Thanks,
Ryan


Re: How to query multi item key

Posted by Paul Davis <pa...@gmail.com>.
> So if I need to query data both ways, any suggestion.

Make two views :)

Paul Davis

Re: How to query multi item key

Posted by Ryan Richins <ri...@mac.com>.
Thanks for the response,

The reason I have it the way I do is the same IP can show up multiple  
times on different dates and the application allows the user to enter  
an IP and a date range and see each time the IP has been entered in  
the db.

So 90% of the queries are something like:

emit([doc['ip'], doc['created_at']], null);

http://localhost:5984/foo/_design/test/_view/by_ip_and_created_at?startkey= 
["27.0.0.1" ,"2009/08/01"]&endkey=["27.0.0.1","2009/08/16"]

Now the customer wants a page that can show ALL ip's in a given date  
range and as a result I was trying to use the views already defined.

So if I need to query data both ways, any suggestion.

Thanks,

Ryan

On Sep 8, 2009, at 2:34 PM, Paul Davis wrote:

>> {"key":["27.0.0.1","2009/08/12 18:37:06 +0000"],"value":null},
>> {"key":["12.0.0.1","2009/08/13 02:29:57 +0000"],"value":null},
>> {"key":["17.0.0.1","2009/08/14 21:25:27 +0000"],"value":null},
>> {"key":["31.0.0.1","2009/08/15 02:54:03 +0000"],"value":null},
>> {"key":["11.0.0.1","2009/08/16 08:35:15 +0000"],"value":null},
>> {"key":["12.0.0.1","2009/08/17 19:21:59 +0000"],"value":null}
>>
>> If I want all of the IP's in a given date range (2009/08/13 -  
>> 2009/08/16)
>> what is the right way to construct the query?  The hang-up is  
>> trying to
>> figure out the right value for the first item in the key to act as  
>> a '*'
>
> That's because you can't. The answer is to swap the order of your  
> emit, thus:
>
> emit([doc.ip, doc.date], null) -> emit([doc.date, doc.ip], null)
>
> And then use:
>
> http://localhost:5984/foo/_design/test/_view/by_ip_and_created_at?startkey= 
> ["2009/08/13",
> null]&endkey=["2009/08/16", {}]
>
> HTH,
> Paul Davis


Re: How to query multi item key

Posted by Paul Davis <pa...@gmail.com>.
> {"key":["27.0.0.1","2009/08/12 18:37:06 +0000"],"value":null},
> {"key":["12.0.0.1","2009/08/13 02:29:57 +0000"],"value":null},
> {"key":["17.0.0.1","2009/08/14 21:25:27 +0000"],"value":null},
> {"key":["31.0.0.1","2009/08/15 02:54:03 +0000"],"value":null},
> {"key":["11.0.0.1","2009/08/16 08:35:15 +0000"],"value":null},
> {"key":["12.0.0.1","2009/08/17 19:21:59 +0000"],"value":null}
>
> If I want all of the IP's in a given date range (2009/08/13 - 2009/08/16)
> what is the right way to construct the query?  The hang-up is trying to
> figure out the right value for the first item in the key to act as a '*'

That's because you can't. The answer is to swap the order of your emit, thus:

emit([doc.ip, doc.date], null) -> emit([doc.date, doc.ip], null)

And then use:

http://localhost:5984/foo/_design/test/_view/by_ip_and_created_at?startkey=["2009/08/13",
null]&endkey=["2009/08/16", {}]

HTH,
Paul Davis