You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Richard Llewellyn <ll...@gmail.com> on 2010/07/21 18:47:37 UTC

a newbie question about querying an exact array field

Hello,

If I have a document with eg doc.my_array = [1,2,3,4,5]

how do I query on the my_array field?

I thought I could write a simple view:

function(doc){
 if (doc.my_array == [1,2,3,4,5])
{ emit(null, doc)
}}

but seems I must be missing something very basic, though I have mucho
googled, as this returns nothing in Futon.  Or is this my ignorance of
javascript?

I see that if I have a doc with a collection field eg: doc.my_collection =
{0:1, 1:2, 2:3, 3:4,4:5},
I can retrieve it by converting the collection to a string

function(doc){
if (doc.my_collection) == "{0:1, 1:2, 2:3, 3:4,4:5}")
....
as long as I am careful to format the string exactly right.   So I could
store lists as collections with explicit indexes and retrieve them that way.
 Is this standard practice?


Thanks,
Rich

Re: a newbie question about querying an exact array field

Posted by Richard Llewellyn <ll...@gmail.com>.
On Wed, Jul 21, 2010 at 3:20 PM, Freddy Bowen <fr...@gmail.com>wrote:

> I don't quite understand your predicament.
>
>
Me neither, apparently!  I was confused about querying views.

Got it.
Thanks for all the help.

Re: a newbie question about querying an exact array field

Posted by "Mark J. Reed" <ma...@gmail.com>.
Good point; I got caught up in the JavaScript issue and overlooked the
fundamental rule of querying couch: don't try to restrict the set of
documents in the map function. Just map the field you care about to a
key.  You can restrict the set of rows at query time.

As far as JSON, map and reduce functions never see JSON - it turns
into actual JavaScript objects before the functions are called.  Most
of the time you shouldn't have to do comparisons yourself, either -
just emit the values and query on them. Couch will do the necessary
comparison work.

On Wed, Jul 21, 2010 at 5:20 PM, Freddy Bowen <fr...@gmail.com> wrote:
> Find your doc in a view:
> http://yourcouch/db/_design/dd/_view/ex?startkey=[1,2,3,4,5]&endkey=[1,2,3,4,5]

or just ?key=[1,2,3,4,5].

-- 
Mark J. Reed <ma...@gmail.com>

Re: a newbie question about querying an exact array field

Posted by Freddy Bowen <fr...@gmail.com>.
I don't quite understand your predicament.

But take the doc:
{"_id": "example1","my_array": [1,2,3,4,5]}

To index your lists just make a view like:
function(doc){ emit(doc.my_array, null); }

Find your doc in a view:
http://yourcouch/db/_design/dd/_view/ex?startkey=[1,2,3,4,5]&endkey=[1,2,3,4,5]

Which returns:
{"total_rows":1,"offset":0,"rows":[
{"id":"example1","key":[1,2,3,4,5],"value":null}
]}

Beware that startkey=[1,2,3,4,5]&endkey=[1,2,3,4,5] matches everything that
sorts in between.

FB



On Wed, Jul 21, 2010 at 4:33 PM, Richard Llewellyn <ll...@gmail.com>wrote:

> Thanks all for quick comments and heads up with javascript!  I was under
> the
> dreamy impression that the array query value would be json encoded
> automagically.
>
> I am storing values with simple keys but json lists and dicts and would
> like
> to index these values by exposing them in a view, not as their individual
> elements (I find the many 'tag' examples) but in their entirety.
>
> >>You'll either have to json encode or hash the array and then compare
> the hashes.
>
> Ok that makes sense, but how do I query with a json encoded value?  I can
> encode an array to json, and the arrays should be json already in the db,
> but how would I write the query?
>
> How do I do what I mean below?  Naively replacing my_array with a json
> string eg "[1, 2, 3, 4, 5]" doesn't work.
>
> eg doc.array = [1,2,3,4,5]
>
> function(doc){
>  if (doc.my_array == my_array in CLIENT ENCODED JSON)
> { emit(doc.my_array, doc);
> }}
>
> Thanks,
> Rich
>

Re: a newbie question about querying an exact array field

Posted by Richard Llewellyn <ll...@gmail.com>.
Thanks all for quick comments and heads up with javascript!  I was under the
dreamy impression that the array query value would be json encoded
automagically.

I am storing values with simple keys but json lists and dicts and would like
to index these values by exposing them in a view, not as their individual
elements (I find the many 'tag' examples) but in their entirety.

>>You'll either have to json encode or hash the array and then compare
the hashes.

Ok that makes sense, but how do I query with a json encoded value?  I can
encode an array to json, and the arrays should be json already in the db,
but how would I write the query?

How do I do what I mean below?  Naively replacing my_array with a json
string eg "[1, 2, 3, 4, 5]" doesn't work.

eg doc.array = [1,2,3,4,5]

function(doc){
 if (doc.my_array == my_array in CLIENT ENCODED JSON)
{ emit(doc.my_array, doc);
}}

Thanks,
Rich

Re: a newbie question about querying an exact array field

Posted by Jason Benesch <ja...@realestatetomato.com>.
Javascript doesn't allow you to compare arrays.

You'll either have to json encode or hash the array and then compare the
hashes.

On Wed, Jul 21, 2010 at 9:47 AM, Richard Llewellyn <ll...@gmail.com>wrote:

> Hello,
>
> If I have a document with eg doc.my_array = [1,2,3,4,5]
>
> how do I query on the my_array field?
>
> I thought I could write a simple view:
>
> function(doc){
>  if (doc.my_array == [1,2,3,4,5])
> { emit(null, doc)
> }}
>
> but seems I must be missing something very basic, though I have mucho
> googled, as this returns nothing in Futon.  Or is this my ignorance of
> javascript?
>
> I see that if I have a doc with a collection field eg: doc.my_collection =
> {0:1, 1:2, 2:3, 3:4,4:5},
> I can retrieve it by converting the collection to a string
>
> function(doc){
> if (doc.my_collection) == "{0:1, 1:2, 2:3, 3:4,4:5}")
> ....
> as long as I am careful to format the string exactly right.   So I could
> store lists as collections with explicit indexes and retrieve them that
> way.
>  Is this standard practice?
>
>
> Thanks,
> Rich
>



-- 
Jason Benesch

We just launched www.TomatoUniversity.com - Join for free!
Technology Training for the Real Estate Industry.

Real Estate Tomato
Co-owner
www.realestatetomato.com
(619) 770-1950
jason@realestatetomato.com

ListingPress
Owner, Founder
www.listingpress.com
(619) 955-7465
jason@listingpress.com

Re: a newbie question about querying an exact array field

Posted by "Mark J. Reed" <ma...@turner.com>.
This is just a JavaScript issue, nothing to do with Couch - you can't 
compare arrays with == in JavaScript:

js> [1,2,3,4,5] == [1,2,3,4,5]
false

To see if two arrays are equivalent, you need to loop through and 
compare them item by item (recursively, in the case of arrays containing 
arrays) - though of course you can fail immediately if they have 
different lengths.

The same restriction applies to general objects:

js> ({'a':1,'b':2,'c':3} == {'a':1,'b':2,'c':3})
false

Basically, two objects are == only if they are identical - two 
references to the same object in memory.  Otherwise, even if their 
contents are the same, == returns false.

With flat arrays of simple values, you can compare string representations:

js> [1,2,3,4,5].toString() == [1,2,3,4,5].toString()
true

But Array.toString() flattens, so [1,2,3,4,5] will also match 
[1,[2,3],[4],5], etc.  And Object.toString() just returns "[object 
Object]", so all objects appear identical.

There are lots of implementations of deep comparison methods out there 
if you do some Googling. Or just write your own for fun/practice. :)

  On 07/21/2010 12:47 PM, Richard Llewellyn wrote:
> Hello,
>
> If I have a document with eg doc.my_array = [1,2,3,4,5]
>
> how do I query on the my_array field?
>
> I thought I could write a simple view:
>
> function(doc){
>   if (doc.my_array == [1,2,3,4,5])
> { emit(null, doc)
> }}
>
> but seems I must be missing something very basic, though I have mucho
> googled, as this returns nothing in Futon.  Or is this my ignorance of
> javascript?
>
> I see that if I have a doc with a collection field eg: doc.my_collection =
> {0:1, 1:2, 2:3, 3:4,4:5},
> I can retrieve it by converting the collection to a string
>
> function(doc){
> if (doc.my_collection) == "{0:1, 1:2, 2:3, 3:4,4:5}")
> ....
> as long as I am careful to format the string exactly right.   So I could
> store lists as collections with explicit indexes and retrieve them that way.
>   Is this standard practice?
>
>
> Thanks,
> Rich
>
>