query for match of all elements in an array

947 views
Skip to first unread message

ricopan

unread,
May 17, 2011, 10:50:04 AM5/17/11
to mongodb-user
I have docs analogous to this:

{ 'values' : [2,3,4] }

{ 'values': [4] }

{ 'values' : [3,4,5,6,7,8,9,10,11] }

I need a query that only returns the doc if ALL elements of the array
match the desired criteria.

Eg. something like (but not)

{ 'values' : { '$gt' : 1, '$lt': 5} })

which would return the first two but not the third doc, as not all
elements of its array 'values' match the criteria.

Andreas Jung

unread,
May 17, 2011, 11:00:44 AM5/17/11
to mongod...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

ricopan wrote:
> I have docs analogous to this:
>
> { 'values' : [2,3,4] }
>
> { 'values': [4] }
>
> { 'values' : [3,4,5,6,7,8,9,10,11] }
>
> I need a query that only returns the doc if ALL elements of the array
> match the desired criteria.
>
> Eg. something like (but not)
>
> { 'values' : { '$gt' : 1, '$lt': 5} })


You can only use the $all operator that requires a full list of needed
values - you can not use a range search here.

- -aj
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQGUBAEBAgAGBQJN0o2bAAoJEADcfz7u4AZjivELwMMX8FxeglwVf+T4NKXRC8kp
6H4yZzoX8GRhv54lra+QGIs0LvHyTGp7E1F4snDVHX08PRTo7yW34+z+ibSAhT/i
SHhjzIJh7R8mEiNwdH1a0uzAgRNQCqGWFuktWW8556vf6m5epsrKXKPquzq3vx22
0YjKQwDCq04qdRywq6xTAzHAzogobiX81kuxjP3cAHKmFfsfZ0OsDmI63wUDPzoP
CBcwbcgIrHeeURfg4lOlZTHn72t62EuokuHTsy632koDw/F2oVKRq+2QEBWZG8wn
zGAhRQ80OVBMDqONgxxGIfXUdS6fXb7xob+bLMhVz0VUK8rOTK7LYVRVVEgGX8sx
a2UjZfkkRzbanBlGT3VpcD9iivwyucVVCnjAaZ8JZD6TmwDfiq9YnS1AWmmIyPTR
ca0jnoSg6rneOd40MDw5QNsRbPJOxleD7sf94nvpPqq5TR2nih82wI40xol1ojt/
q8cmdRJqFzNjC9hnamlzYPKLZNNSYP0=
=pUQM
-----END PGP SIGNATURE-----

lists.vcf

ricopan

unread,
May 17, 2011, 11:07:54 AM5/17/11
to mongodb-user
Yeah, the $all operator is clearly not what I need. I need to match
all values in the doc array, not in the query.

I suppose in the example I gave one might be able to write a query for
the negation ($lt:1,$gt:5) and use $not, but I need a more general
case where the negation is not known.

On May 17, 9:00 am, Andreas Jung <li...@zopyx.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> ricopan wrote:
> > I have docs analogous to this:
>
> > { 'values' : [2,3,4] }
>
> > { 'values': [4] }
>
> > { 'values' : [3,4,5,6,7,8,9,10,11] }
>
> > I need a query that only returns the doc if ALL elements of the array
> > match the desired criteria.
>
> > Eg.   something like (but not)
>
> > { 'values' : { '$gt' : 1, '$lt': 5} })
>
> You can only use the $all operator that requires a full list of needed
> values - you can not use a range search here.
>
> - -aj
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.11 (Darwin)
> Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org/
>
> iQGUBAEBAgAGBQJN0o2bAAoJEADcfz7u4AZjivELwMMX8FxeglwVf+T4NKXRC8kp
> 6H4yZzoX8GRhv54lra+QGIs0LvHyTGp7E1F4snDVHX08PRTo7yW34+z+ibSAhT/i
> SHhjzIJh7R8mEiNwdH1a0uzAgRNQCqGWFuktWW8556vf6m5epsrKXKPquzq3vx22
> 0YjKQwDCq04qdRywq6xTAzHAzogobiX81kuxjP3cAHKmFfsfZ0OsDmI63wUDPzoP
> CBcwbcgIrHeeURfg4lOlZTHn72t62EuokuHTsy632koDw/F2oVKRq+2QEBWZG8wn
> zGAhRQ80OVBMDqONgxxGIfXUdS6fXb7xob+bLMhVz0VUK8rOTK7LYVRVVEgGX8sx
> a2UjZfkkRzbanBlGT3VpcD9iivwyucVVCnjAaZ8JZD6TmwDfiq9YnS1AWmmIyPTR
> ca0jnoSg6rneOd40MDw5QNsRbPJOxleD7sf94nvpPqq5TR2nih82wI40xol1ojt/
> q8cmdRJqFzNjC9hnamlzYPKLZNNSYP0=
> =pUQM
> -----END PGP SIGNATURE-----
>
>  lists.vcf
> < 1KViewDownload

Andreas Jung

unread,
May 17, 2011, 11:18:47 AM5/17/11
to mongod...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

You have been asking about

"""
I need a query that only returns the doc if ALL elements of the array
match the desired criteria.
"""

and not about a negation....completely different topic.

- -aj

ricopan wrote:
> Yeah, the $all operator is clearly not what I need. I need to match
> all values in the doc array, not in the query.
>
> I suppose in the example I gave one might be able to write a query for
> the negation ($lt:1,$gt:5) and use $not, but I need a more general
> case where the negation is not known.
>
> On May 17, 9:00 am, Andreas Jung <li...@zopyx.com> wrote:
> ricopan wrote:
>>>> I have docs analogous to this:
>>>> { 'values' : [2,3,4] }
>>>> { 'values': [4] }
>>>> { 'values' : [3,4,5,6,7,8,9,10,11] }
>>>> I need a query that only returns the doc if ALL elements of the array
>>>> match the desired criteria.
>>>> Eg. something like (but not)
>>>> { 'values' : { '$gt' : 1, '$lt': 5} })
> You can only use the $all operator that requires a full list of needed
> values - you can not use a range search here.
>

> -aj
>>
lists.vcf
< 1KViewDownload

- --
ZOPYX Limited | zopyx group
Charlottenstr. 37/1 | The full-service network for Zope & Plone
D-72070 T�bingen | Produce & Publish
www.zopyx.com | www.produce-and-publish.com
- ------------------------------------------------------------------------
E-Publishing, Python, Zope & Plone development, Consulting


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQGUBAEBAgAGBQJN0pHWAAoJEADcfz7u4AZjUagLvi84CLbkjzq3IadlbccT88rN
4AtbJHkEGy3vdMijv4yaHmHvTg072WV1VsD0oE8xeHfJnPhSj7jxyGDmAKAMZZTi
xi+vxbleF3TxMybgJUFXeg4T6go7iSa8xg7v0wz4R+OLbd2nyMyiCsCOyNHYrpa0
ofuBr2TCrb+JfY3vKGmUF+warEs7XHbojCOwsVdQeLaW9D8TNOzq/pG+I63Eb+EP
mY/SAvL8WDQh+WarQQm/N5Lgh3p1m/HQ/VSsgwbiKnfLz3Spnfuch1ul+ZvZJHTK
5xKB4AOyGuA1r6LVzrzve0+o4YpevZqbxXk9ghVQMLEBtG7gimW7OgIwbtUGyHyG
aLwB4A7eMkzyg2CmSzOmSSj4Y7hR+hmre8/Qkc/nQVipFEs84lEJU+06khD7WCVr
eICe/nrytpOTdwHCyE85c0zDYCDHDwRK4DRVz2VRYQIqJ+vuEOilR4ZfHzy2zoz5
2IetLrH9M9CNrmlqYC0tv0JQ339Lwmc=
=+TxC
-----END PGP SIGNATURE-----

lists.vcf

ricopan

unread,
May 17, 2011, 11:42:06 AM5/17/11
to mongodb-user
to clarify, this is not a question about the $all operator. Negation
is not a general solution, so let's move on.

Perhaps the question is too simple and the answer too obvious (I
hope). I'm new to mongo.

How can I write a query that requires all elements in a doc's array to
match a specified criteria? In general, a doc is returned if ANY
element of the array matches, rather than ALL elements.
> D-72070 T�bingen        | Produce & Publishwww.zopyx.com          |www.produce-and-publish.com
> - ------------------------------------------------------------------------
> E-Publishing, Python, Zope & Plone development, Consulting
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.11 (Darwin)
> Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org/
>
> iQGUBAEBAgAGBQJN0pHWAAoJEADcfz7u4AZjUagLvi84CLbkjzq3IadlbccT88rN
> 4AtbJHkEGy3vdMijv4yaHmHvTg072WV1VsD0oE8xeHfJnPhSj7jxyGDmAKAMZZTi
> xi+vxbleF3TxMybgJUFXeg4T6go7iSa8xg7v0wz4R+OLbd2nyMyiCsCOyNHYrpa0
> ofuBr2TCrb+JfY3vKGmUF+warEs7XHbojCOwsVdQeLaW9D8TNOzq/pG+I63Eb+EP
> mY/SAvL8WDQh+WarQQm/N5Lgh3p1m/HQ/VSsgwbiKnfLz3Spnfuch1ul+ZvZJHTK
> 5xKB4AOyGuA1r6LVzrzve0+o4YpevZqbxXk9ghVQMLEBtG7gimW7OgIwbtUGyHyG
> aLwB4A7eMkzyg2CmSzOmSSj4Y7hR+hmre8/Qkc/nQVipFEs84lEJU+06khD7WCVr
> eICe/nrytpOTdwHCyE85c0zDYCDHDwRK4DRVz2VRYQIqJ+vuEOilR4ZfHzy2zoz5
> 2IetLrH9M9CNrmlqYC0tv0JQ339Lwmc=
> =+TxC
> -----END PGP SIGNATURE-----
>
>  lists.vcf
> < 1KViewDownload

mcoolin

unread,
May 17, 2011, 12:56:42 PM5/17/11
to mongod...@googlegroups.com
Check out this page: http://www.mongodb.org/display/DOCS/Advanced+Queries
 and here http://www.mongodb.org/display/DOCS/Dot+Notation+%28Reaching+into+Objects%29

They have a section on array matching.
The second link has a sample of what I believe you want.
db.foo.find({"foo.shape": "square", "foo.color": "purple"})
It would be nice if they merged some of this array information into a single document. Took me hours to find it last night.
Hope this helps ;)

shekhar

unread,
May 17, 2011, 12:44:36 PM5/17/11
to mongodb-user

http://www.mongodb.org/display/DOCS/Full+Text+Search+in+Mongo#FullTextSearchinMongo-Multikeys%28IndexingValuesinanArray%29

Is this what you were looking for? I am still thrown off a bit by your
question, but still, see if the above helps.

shekhar

ricopan

unread,
May 17, 2011, 2:09:32 PM5/17/11
to mongodb-user
Thanks mcoolin and shekhar.

I think my problem is just too simple and I'm not asking it well, or
this is so obvious that it is being misinterpreted.

I want an array that will return a doc if all elements the doc's array
match some criteria.

Another example:

docA = {'_id': 'foo', 'some_array': ['red','yellow','blue']}
docB = {'_id': 'bar', 'some_array': ['red','yellow','green']}

so say in this case I want to ensure that all elements of some_array
are either red,yellow or blue (docA but not docB).

This query

collection.find({'some_array': {$in:['red','yellow','blue']}})

will return both docA and docB, because the query returns a doc, if
I understand correctly, if any element of the doc's array matches (and
the first two, red and yellow, match the $in query).

From the links you gave above, I think I could manually index every
element of the array eg

c = ['red','yellow','blue']
collection.find({'some_array.0': {$in:c}, 'some_array.1':
{$in:c},'some_array.2':{$in:c}})

but this is a pain, especially as I'm dealing with a highly dynamic
enviro.







On May 17, 10:44 am, shekhar <shekh...@gmail.com> wrote:
> http://www.mongodb.org/display/DOCS/Full+Text+Search+in+Mongo#FullTex...

ricopan

unread,
May 17, 2011, 2:31:23 PM5/17/11
to mongod...@googlegroups.com
Dangitall!   Now you should really be confused. Third paragraph should read:

"I want a query that will return a doc if all elements of the doc's array 
match some criteria."


rather than

mcoolin

unread,
May 17, 2011, 3:10:16 PM5/17/11
to mongod...@googlegroups.com
I tried this with you sample data and it worked.
 db.rico.find({'some_array' : { $all: [ 'red', 'blue', 'yellow'] }})

mcoolin

unread,
May 17, 2011, 3:24:37 PM5/17/11
to mongod...@googlegroups.com
P.S. I tried a different order, the order does not have to match.

ricopan

unread,
May 17, 2011, 3:34:14 PM5/17/11
to mongodb-user
Thanks for testing that!
Unfortunately that was a bad example because it got us back to the
$all operator.
Ok, so my understanding is that the $all operator tests that all
elements in the Query array {$all:[red,blue,yellow]} are found in the
doc array; that is, the query array is a subset of the doc array. So
if I add another doc

docC = {'_id': 'baz', 'some_array':
['red','yellow','blue','purple']}

this will also be matched by your $all array, but is not what I want
because purple is not an element of red, yellow, or blue.

I guess another way to say this is that it appears that queries on doc
arrays implicitly are OR over all elements, where I need AND over all
elements.

Andreas Jung

unread,
May 17, 2011, 11:44:32 PM5/17/11
to mongod...@googlegroups.com
I really have no idea what you are actually asking. Every subsequent posting constains a piece of information being not consistent with former postings...you're confusing us..please sit back for a moment and make another try for explaining what you *have* and what you really need as a result of query.

-aj

lists.vcf

Scott Hernandez

unread,
May 17, 2011, 11:51:57 PM5/17/11
to mongod...@googlegroups.com
On Tue, May 17, 2011 at 3:34 PM, ricopan <lle...@gmail.com> wrote:
Thanks for testing that!
Unfortunately that was a bad example because it got us back to the
$all operator.
Ok, so my understanding is that the $all operator tests that all
elements in the Query array {$all:[red,blue,yellow]} are found in the
doc array; that is, the query array is a subset of the doc array.  So

Yes, the query $all items must be a subset or exact match. Do you want an exact match only?
 
if I add another doc

docC  =  {'_id': 'baz', 'some_array':
['red','yellow','blue','purple']}

this will also be matched by your $all array, but is not what I want
because purple is not an element of red, yellow, or blue.

I guess another way to say this is that it appears that queries on doc
arrays implicitly are OR over all elements, where I need AND over all
elements.

$all is an AND, while $in is OR (and the same as the single item comparison which is basically an $in of that one element).
 

On May 17, 1:10 pm, mcoolin <mcoo...@techie.com> wrote:
> I tried this with you sample data and it worked.
>  db.rico.find({'some_array' : { $all: [ 'red', 'blue', 'yellow'] }})

--
You received this message because you are subscribed to the Google Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com.
To unsubscribe from this group, send email to mongodb-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mongodb-user?hl=en.


Andreas Jung

unread,
May 18, 2011, 12:05:32 AM5/18/11
to mongod...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Scott Hernandez wrote:
>
>
> On Tue, May 17, 2011 at 3:34 PM, ricopan <lle...@gmail.com
> <mailto:lle...@gmail.com>> wrote:
>
> Thanks for testing that!
> Unfortunately that was a bad example because it got us back to the
> $all operator.
> Ok, so my understanding is that the $all operator tests that all
> elements in the Query array {$all:[red,blue,yellow]} are found in the
> doc array; that is, the query array is a subset of the doc array. So
>
>
> Yes, the query $all items must be a subset or exact match. Do you want
> an exact match only?
>
>
> if I add another doc
>
> docC = {'_id': 'baz', 'some_array':
> ['red','yellow','blue','purple']}
>
> this will also be matched by your $all array, but is not what I want
> because purple is not an element of red, yellow, or blue.
>
> I guess another way to say this is that it appears that queries on doc
> arrays implicitly are OR over all elements, where I need AND over all
> elements.
>
>
> $all is an AND, while $in is OR

That's what the names of the operators implictely imply and the
documentation and examples under

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24all

are pretty clear...that's what I already explained in my first reply.

- -aj


-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQGUBAEBAgAGBQJN00WMAAoJEADcfz7u4AZjka0Lvjf2lunn9p3BEjm3CNq+S+d5
RBodK/tSZc24XFTxAao2wMrP1CwySKrtiHezr2M104ZknZDTuexwDduHCUs+6Ocy
FbGTtWPAZsmLQITBuQZJm3jysL4YNJn01VN8/NGwAT7uZVTRL2DHHr6aMI1ikX6I
Z7Ax6qzWJ11H63LF7BHt3c/ik7LRpRkQWe++wAg+pyySogfpfTpZrISLsQvmVfjI
86HqpOVkhPUxuXfvP0s2a35ae+kcB9Ju7rpsKOSudR9CEYzwwGnNP4ejfY4rRo3y
sG6QN9JYH+4EjSga7t0bIghupCNwIP1M2RsM0iIjxOF2Od8OKw+XUNsCAvNlBjwH
9tTlIz+2+aAhsligZ4EIrnjQI4YDJEKs7NVHxjP0nuNFWFqgBMQ0D20OA79GjuV9
O2X004k2lJzM9mFNWRJROs4SmPF1x0I+AN4r2EsgGZ/r0WVctiuhz4CnYucPlGLE
UK09pk6MEctCzDxRirydleqeZEVxhxI=
=9KHk
-----END PGP SIGNATURE-----

lists.vcf

ricopan

unread,
May 18, 2011, 1:20:41 AM5/18/11
to mongodb-user
Wow guys, this really isn't that tough to understand.

Go back to my original question and ignore subsequent postings that
are confounding $all with the question. This has nothing to do with
$all. I got better results over at stackoverflow:
http://stackoverflow.com/questions/6038818/mongodb-query-to-match-each-element-in-doc-array-to-a-condition
suggesting javascript.

Seems like a certain disgruntled fellow here has followed me over
there....

On May 17, 10:05 pm, Andreas Jung <li...@zopyx.com> wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
>
>
>
>
>
>
>
>
> Scott Hernandez wrote:
>
> > On Tue, May 17, 2011 at 3:34 PM, ricopan <llew...@gmail.com
> > <mailto:llew...@gmail.com>> wrote:
>
> >     Thanks for testing that!
> >     Unfortunately that was a bad example because it got us back to the
> >     $all operator.
> >     Ok, so my understanding is that the $all operator tests that all
> >     elements in the Query array {$all:[red,blue,yellow]} are found in the
> >     doc array; that is, the query array is a subset of the doc array.  So
>
> > Yes, the query $all items must be a subset or exact match. Do you want
> > an exact match only?
>
> >     if I add another doc
>
> >     docC  =  {'_id': 'baz', 'some_array':
> >     ['red','yellow','blue','purple']}
>
> >     this will also be matched by your $all array, but is not what I want
> >     because purple is not an element of red, yellow, or blue.
>
> >     I guess another way to say this is that it appears that queries on doc
> >     arrays implicitly are OR over all elements, where I need AND over all
> >     elements.
>
> > $all is an AND, while $in is OR
>
> That's what the names of the operators implictely imply and the
> documentation and examples under
>
> http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-...
>
> are pretty clear...that's what I already explained in my first reply.
>
> - -aj
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.11 (Darwin)
> Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org/
>
> iQGUBAEBAgAGBQJN00WMAAoJEADcfz7u4AZjka0Lvjf2lunn9p3BEjm3CNq+S+d5
> RBodK/tSZc24XFTxAao2wMrP1CwySKrtiHezr2M104ZknZDTuexwDduHCUs+6Ocy
> FbGTtWPAZsmLQITBuQZJm3jysL4YNJn01VN8/NGwAT7uZVTRL2DHHr6aMI1ikX6I
> Z7Ax6qzWJ11H63LF7BHt3c/ik7LRpRkQWe++wAg+pyySogfpfTpZrISLsQvmVfjI
> 86HqpOVkhPUxuXfvP0s2a35ae+kcB9Ju7rpsKOSudR9CEYzwwGnNP4ejfY4rRo3y
> sG6QN9JYH+4EjSga7t0bIghupCNwIP1M2RsM0iIjxOF2Od8OKw+XUNsCAvNlBjwH
> 9tTlIz+2+aAhsligZ4EIrnjQI4YDJEKs7NVHxjP0nuNFWFqgBMQ0D20OA79GjuV9
> O2X004k2lJzM9mFNWRJROs4SmPF1x0I+AN4r2EsgGZ/r0WVctiuhz4CnYucPlGLE
> UK09pk6MEctCzDxRirydleqeZEVxhxI=
> =9KHk
> -----END PGP SIGNATURE-----
>
>  lists.vcf
> < 1KViewDownload

ricopan

unread,
May 18, 2011, 1:28:50 AM5/18/11
to mongodb-user
Thanks, but this really isn't about the $and operator. I need to
match each (all) elements in the doc's array (not an array of the
query) with some condition. But I got my answer over at
stackoverflow.

On May 17, 9:51 pm, Scott Hernandez <scotthernan...@gmail.com> wrote:

mcoolin

unread,
May 18, 2011, 10:50:10 AM5/18/11
to mongod...@googlegroups.com
You think this would be an easy query, I did figure out how to do this :P

Let me summarize:
Given:
db.rico.insert({ "_id" : "foo", "some_array" : [ "red", "yellow", "blue" ] });
db.rico.insert({ "_id" : "bar", "some_array" : [ "red", "yellow", "green" ] });
db.rico.insert({ "_id" : "baz", "some_array" : [ "red", "yellow", "blue", "purple" ] })

 db.rico.find({'some_array' : { $all: [ 'red', 'blue', 'yellow'] }})
 returns:
 { "_id" : "foo", "some_array" : [ "red", "yellow", "blue" ] }
{ "_id" : "baz", "some_array" : [ "red", "yellow", "blue", "purple" ] }
// good but it also returns record with purple, not desired

 db.rico.find({some_array: {$size: 3}})
 returns:
 { "_id" : "foo", "some_array" : [ "red", "yellow", "blue" ] }
{ "_id" : "bar", "some_array" : [ "red", "yellow", "green" ] }

 db.rico.find({some_array: {$size: 3}, 'some_array' : { $all: [ 'red', 'blue', 'yellow'] } })
 // would seen to be right but returns the wrong items - Is this a bug?
 { "_id" : "foo", "some_array" : [ "red", "yellow", "blue" ] }
{ "_id" : "baz", "some_array" : [ "red", "yellow", "blue", "purple" ] }


// using a functuion
f = function() { 
var rv = true,
a = [ 'red', 'blue', 'yellow'],
mc = 0, c, i;
for(c=0; c<a.length ; c++){
i=this.some_array.length;
while (i--) {
if (this.some_array[i] === a[c]) {
mc++
}
}
}
print(mc)
if(mc != this.some_array.length){
rv=false;
}
return rv;
}; 
db.rico.find(f);

What I don't know is how you can pass in an argument to the function to provide your original array.


Andreas Jung

unread,
May 18, 2011, 11:01:07 AM5/18/11
to mongod...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

mcoolin wrote:

> db.rico.find({some_array: {$size: 3}, 'some_array' : { $all: [ 'red',
> 'blue', 'yellow'] } })
> // would seen to be right but returns the wrong items - Is this a bug?

You can not specify the same key twice within a JS array. Once
definition will override the other one - basic JS know-how.


>
> // using a functuion
> f = function() {
> var rv = true,
> a = [ 'red', 'blue', 'yellow'],
> mc = 0, c, i;
> for(c=0; c<a.length ; c++){
> i=this.some_array.length;
> while (i--) {
> if (this.some_array[i] === a[c]) {
> mc++
> }
> }
> }
> print(mc)
> if(mc != this.some_array.length){
> rv=false;
> }
> return rv;
> };
> db.rico.find(f);

Server-side execution of code should be avoided since it will be much
slover - especially because indexes won't take into account. So this is
neither scalable nor advicable for large collections.

- -aj
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQGUBAEBAgAGBQJN098yAAoJEADcfz7u4AZjd7ULwMZgqEmGSV4e5qHDiL1tL3LW
p4FgfxoyAZFCPhzWYDBHDHbIboaqZVSxG174SRn24oC4pnX/eqfpQko6LV/CsFh7
cLebcu2jrAvwyAnnx6jdW4tXfvWKoZ6j7Jyy8TwYqLkDgjVWaiVT8IiwIK3koj76
hypbFzJFtQJajPgIL7QlMK/OWubGpfIwqSi6oonKX0CFTBCuhO+6Uvxkrr8v0Hle
i6eRgxkoKstHdl/QbSlqG6EIkPgRfABUl/nYatLzARmmMls7m/QeMa+2EhHyOfHD
djEAge+ArXYCG73A/nUjiL/T6lgF9oiBUf0Yq7dP7nezY4G+MfdCZTjcdwGWnO2G
DyiMP2CcUkvXdjEiwOR65NTMB5+BufRPvuxbdoKnCPvAM+UoJRuyVlQsHrR2a7tN
H1YRIoUURB9Wujewgw2ykVB/IPde9zlQS4xyAxXQ178nuIr8oKsXXzYZ/IW5nG/b
NxTRf+5VWo4GsVDP9nhc9pPwvDMf7N8=
=yR+u
-----END PGP SIGNATURE-----

lists.vcf

mcoolin

unread,
May 18, 2011, 11:08:35 AM5/18/11
to mongod...@googlegroups.com, li...@zopyx.com
Andreas,
Just to be clear, I'm not referring to elements in the array but to the call of the function.
db.rico.find(f); <- ideally there would be a means to pass in the function and a variable. 'this' refers to the current object, but I think a means should be available to pass a variable. In this example the array being searched for.

Ideally it should be something like:
db.rico.find(f,v)

Andreas Jung

unread,
May 18, 2011, 11:29:21 AM5/18/11
to mongod...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I just give up.

- -aj

> --
> You received this message because you are subscribed to the Google
> Groups "mongodb-user" group.
> To post to this group, send email to mongod...@googlegroups.com.
> To unsubscribe from this group, send email to
> mongodb-user...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/mongodb-user?hl=en.

- --
ZOPYX Limited | zopyx group
Charlottenstr. 37/1 | The full-service network for Zope & Plone

D-72070 T�bingen | Produce & Publish

www.zopyx.com | www.produce-and-publish.com


- ------------------------------------------------------------------------
E-Publishing, Python, Zope & Plone development, Consulting

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQGUBAEBAgAGBQJN0+XRAAoJEADcfz7u4AZjkKgLvjTIvoTBscL7ZLdNHey3fNOT
pHzuBrrFs7ZEjVurSCAKU+PhR6vJpvZBDQ5a0jCtNvXOc227FsVqovKa8rIetnB+
Kzvv4vVkWzp8Ia02KzvS0Jw8gK8YKa+tXUq5qY4t2h2y3vYUF8NedGaD33369WLH
4NFVVjhTCRR0u3Qqtp8J2x9h/lNOhNv3qJUbeQofCGucIfdCI1M0Fu1JN4KhIueY
8vlJE1ZBn0oS/7Qr102pKr4kj2mac6TeRafahkWj7OArDnkIUlp5YcktlSC4U17T
xBNvnY42rIG8fghuA2rlaguI1z1Kfgn7+dvukKnXF5Qizs9PE29EeiwVdf+jgiX5
TP9mBwBqvjYTtjhAB9ck23Oadh85C6FKa5E5p9Vol0DWa9pCT5n5lDkn4+G8OCbn
+MKnw73bqkHvoIvx4K24On+R5TLoNhEDluYeautJFZafHQjdlIdRk+/Sczti0QbE
YnE4B6VutxPSmorlx/Z46Ev99snFNv0=
=mwMj
-----END PGP SIGNATURE-----

lists.vcf

Robert Stam

unread,
May 18, 2011, 12:24:48 PM5/18/11
to mongodb-user
@ricopan, for what it's worth, I thought your question was very
straightforward. I agree that at the moment there is no easy way to do
what you need except by using a $where query, which will have to scan
the entire collection because $where queries can't use indexes.

For those who don't want to piece together the bits and pieces of
answers from here and there, here's a mongo shell transcript showing
the solution offered by the good folks on stackoverflow:

From: http://stackoverflow.com/questions/6038818/mongodb-query-to-match-each-element-in-doc-array-to-a-condition

> db.test.find()
{ "_id" : ObjectId("4dd3ee6b3c139aff4915373a"), "values" : [ 4 ] }
{ "_id" : ObjectId("4dd3ee673c139aff49153739"), "values" : [ 2, 3,
4 ] }
{ "_id" : ObjectId("4dd3ee733c139aff4915373b"), "values" : [ 3, 4, 5,
6, 7, 8, 9, 10, 11 ] }
>
> filter = function() { return this.values.every(function(v) { return v > 1 && v < 5; }) }
function () {
return this.values.every(function (v) {return v > 1 && v < 5;});
}
>
> db.test.find({ $where : filter })
{ "_id" : ObjectId("4dd3ee6b3c139aff4915373a"), "values" : [ 4 ] }
{ "_id" : ObjectId("4dd3ee673c139aff49153739"), "values" : [ 2, 3,
4 ] }
>

(slightly altered from the stackoverflow answers, using a first class
function object and an explicit $where query).

Thanks for trying out MongoDB. Sorry you had to struggle through a
frustrating discussion to get your answer! :-)
> D-72070 T�bingen        | Produce & Publishwww.zopyx.com          |www.produce-and-publish.com
> - ------------------------------------------------------------------------
> E-Publishing, Python, Zope & Plone development, Consulting
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.11 (Darwin)
> Comment: Using GnuPG with Mozilla -http://enigmail.mozdev.org/
>
> iQGUBAEBAgAGBQJN0+XRAAoJEADcfz7u4AZjkKgLvjTIvoTBscL7ZLdNHey3fNOT
> pHzuBrrFs7ZEjVurSCAKU+PhR6vJpvZBDQ5a0jCtNvXOc227FsVqovKa8rIetnB+
> Kzvv4vVkWzp8Ia02KzvS0Jw8gK8YKa+tXUq5qY4t2h2y3vYUF8NedGaD33369WLH
> 4NFVVjhTCRR0u3Qqtp8J2x9h/lNOhNv3qJUbeQofCGucIfdCI1M0Fu1JN4KhIueY
> 8vlJE1ZBn0oS/7Qr102pKr4kj2mac6TeRafahkWj7OArDnkIUlp5YcktlSC4U17T
> xBNvnY42rIG8fghuA2rlaguI1z1Kfgn7+dvukKnXF5Qizs9PE29EeiwVdf+jgiX5
> TP9mBwBqvjYTtjhAB9ck23Oadh85C6FKa5E5p9Vol0DWa9pCT5n5lDkn4+G8OCbn
> +MKnw73bqkHvoIvx4K24On+R5TLoNhEDluYeautJFZafHQjdlIdRk+/Sczti0QbE
> YnE4B6VutxPSmorlx/Z46Ev99snFNv0=
> =mwMj
> -----END PGP SIGNATURE-----
>
>  lists.vcf
> < 1KViewDownload
Reply all
Reply to author
Forward
0 new messages