Re: [mongodb-user] Query by Array size in Java driver

591 views
Skip to first unread message

Octavian Covalschi

unread,
Sep 11, 2012, 5:24:26 PM9/11/12
to mongod...@googlegroups.com
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24size 

On Tue, Sep 11, 2012 at 4:06 PM, shekhar agrawal <shekha...@gmail.com> wrote:
I am using this query in Mongo shell and getting desired result.

db.MyCollection.find( {"owner":"abc" , $where: "if ( ( this.myArray && this.myArray.length > 1 ) ) { return this; } " } ).count();

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 mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

Rob Moore

unread,
Sep 11, 2012, 9:47:29 PM9/11/12
to mongod...@googlegroups.com

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 that turns into:

    DocumentAssignable query = QueryBuilder.where("owner").equals("abc")
                                                                  .and("myArray.1").exists();

Rob

Jenna deBoisblanc

unread,
Sep 12, 2012, 6:36:51 PM9/12/12
to mongodb-user
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/...>
Reply all
Reply to author
Forward
0 new messages