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
Converting Mongo OR query to Java language query
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
  3 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
 
Christopher Carver  
View profile  
 More options Oct 12 2012, 11:23 am
From: Christopher Carver <christopher.car...@gmail.com>
Date: Fri, 12 Oct 2012 08:23:55 -0700 (PDT)
Local: Fri, Oct 12 2012 11:23 am
Subject: Converting Mongo OR query to Java language query

I have the following MongoDB query that I need to convert to Java language.

db.someCollection.count( { $or :  [ { 'WORD' : 'NICE' } , { 'WORD' :
'HAPPY' } , { 'WORD' : 'BAD' } ] } );

I'm stuck on how to stipulate the the OR condition as an array in the
BasicDBObject. Not sure what the proper approach for this.

BasicDBObject cond1 = new BasicDBObject( "WORD", "NICE" );

BasicDBObject cond2 = new BasicDBObject( "WORD", "HAPPY" );

BasicDBObject cond3 = new BasicDBObject( "WORD", "BAD" );

BasicDBObject query = new BasicDBObject();

query.put("$or", ???? );


 
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.
Sid  
View profile  
 More options Oct 12 2012, 4:42 pm
From: Sid <siddharth.si...@10gen.com>
Date: Fri, 12 Oct 2012 13:42:19 -0700 (PDT)
Local: Fri, Oct 12 2012 4:42 pm
Subject: Re: Converting Mongo OR query to Java language query
Something like this should work :

ArrayList orList = new ArrayList();

orList.add(new BasicDBObject("WORD", "NICE"));
orList.add(new BasicDBObject("WORD", "HAPPY"));
orList.add(new BasicDBObject("WORD", "BAD"));

BasicDBObject query = new BasicDBObject("$or", orList);

On Oct 12, 11:23 am, Christopher Carver <christopher.car...@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.
Steve Briskin  
View profile  
 More options Oct 12 2012, 4:55 pm
From: Steve Briskin <steve.bris...@10gen.com>
Date: Fri, 12 Oct 2012 13:55:09 -0700 (PDT)
Local: Fri, Oct 12 2012 4:55 pm
Subject: Re: Converting Mongo OR query to Java language query

I addition to Sid's solution, you can do any of these.  I'm using the
QueryBuilder, but all of these queries can be constructed with
BasicDBObject as well.

        DBObject query = QueryBuilder.start().or(cond1, cond2, cond3).get();

If you have a List of conditions:

        DBObject query = QueryBuilder.start().or(orList.toArray(new
DBObject[]{})).get();
Since you're querying for values of the same field, you can use $in instead
of $or:

      List values = Arrays.asList("NICE", "HAPPY", "BAD");

      DBObject query2 = QueryBuilder.start("WORD").in(values).get();


 
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 »