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


Reply via email to