The best way to optimize this query is to store a counter field and
$inc the value every time you add an element to the array. $where uses
Javascript, which is single-threaded and is potentially slow.
If possible, you may want to look into the new aggregation framework,
included in MongoDB version 2.2, which may have better performance
than $where since it doesn't rely on Javascript:
http://docs.mongodb.org/manual/applications/aggregation/
On Sep 11, 9:47 pm, Rob Moore <
robert.allanb...@gmail.com> wrote:
> So the $size operator only matches if the size is exact.
>
> You can achieve the effect you want using the $exists operator and the "."
> operator:
>
> The shell code is like:
> db.MyCollection.find( {"owner":"abc" , "myArray.1" : { "$exists" : true
>
> } } )
>
> Basically saying "match an array with a second element". Indexes are zero
> based.
>
> The Java side looks like:
> BasicDBObject query = new BasicDBObject("owner", "abc");
> query.put("myArray.1", new BasicDBObject("$exists", Boolean.TRUE) );
>
> If you use the Asynchronous driver<
http://www.allanbank.com/mongodb-async-driver/>that turns into:
>
> DocumentAssignable query = QueryBuilder<
http://www.allanbank.com/mongodb-async-driver/apidocs/index.html?com/...>