> Return all documents where the array "myArray" is not null and its size is
> greater than 1.
> I am finding it hard to implement this in Java.
> BasicDBObject query = new BasicDBObject("owner", "abc");
> query.put( "myArray", new BasicDBObject("$ne", null) );
> query.put("myArray", new BasicDBObject("$size", 2) );
> How could I query for all documents having the size of the array "to"
> greater than 1?
> Thanks,
> Shekhar
> --
> You received this message because you are subscribed to the Google
> Groups "mongodb-user" group.
> To post to this group, send email to mongodb-user@googlegroups.com
> To unsubscribe from this group, send email to
> mongodb-user+unsubscribe@googlegroups.com
> See also the IRC channel -- freenode.net#mongodb
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) );
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) );