Converting Mongo OR query to Java language query
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:
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.
You do not have the permission required to post.
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:
> 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.
You do not have the permission required to post.
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();
On Friday, October 12, 2012 11:23:55 AM UTC-4, Christopher Carver wrote:
> 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.
You do not have the permission required to post.