Date: Tue, 11 Sep 2012 14:06:20 -0700 (PDT) From: shekhar agrawal To: mongodb-user@googlegroups.com Message-Id: Subject: Query by Array size in Java driver MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_Part_697_8669257.1347397580590" ------=_Part_697_8669257.1347397580590 Content-Type: multipart/alternative; boundary="----=_Part_698_10874216.1347397580590" ------=_Part_698_10874216.1347397580590 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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 ------=_Part_698_10874216.1347397580590 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit
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
------=_Part_698_10874216.1347397580590-- ------=_Part_697_8669257.1347397580590--