Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Query by Array size in Java driver
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Rob Moore  
View profile  
 More options Sep 11 2012, 9:47 pm
From: Rob Moore <robert.allanb...@gmail.com>
Date: Tue, 11 Sep 2012 18:47:29 -0700 (PDT)
Local: Tues, Sep 11 2012 9:47 pm
Subject: Re: Query by Array size in Java driver

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/...>
.where("owner").equals("abc")

.and("myArray.1").exists();

Rob

On Tuesday, September 11, 2012 5:06:20 PM UTC-4, shekhar agrawal 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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.