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
Query by Array size in Java driver
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
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
 
shekhar agrawal  
View profile  
 More options Sep 11 2012, 5:06 pm
From: shekhar agrawal <shekhar201...@gmail.com>
Date: Tue, 11 Sep 2012 14:06:20 -0700 (PDT)
Local: Tues, Sep 11 2012 5:06 pm
Subject: Query by Array size in Java driver

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.
Octavian Covalschi  
View profile  
 More options Sep 11 2012, 5:25 pm
From: Octavian Covalschi <octavian.covals...@gmail.com>
Date: Tue, 11 Sep 2012 16:24:26 -0500
Local: Tues, Sep 11 2012 5:24 pm
Subject: Re: [mongodb-user] Query by Array size in Java driver

http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-...

On Tue, Sep 11, 2012 at 4:06 PM, shekhar agrawal <shekhar201...@gmail.com>wrote:


 
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.
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


 
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.
Jenna deBoisblanc  
View profile  
 More options Sep 12 2012, 6:37 pm
From: Jenna deBoisblanc <jenna.deboisbl...@10gen.com>
Date: Wed, 12 Sep 2012 15:36:51 -0700 (PDT)
Local: Wed, Sep 12 2012 6:36 pm
Subject: Re: Query by Array size in Java driver
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:


 
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.
End of messages
« Back to Discussions « Newer topic     Older topic »