Skip to content

Commit

Permalink
Merge pull request #55 from wfreeman/master
Browse files Browse the repository at this point in the history
This adds $and support to QueryBuilder, along with a simple test.
  • Loading branch information
Brendan W. McAdams committed Jan 25, 2012
2 parents f4a2601 + 4585794 commit 94a5fda
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/com/mongodb/QueryBuilder.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -278,6 +278,23 @@ public QueryBuilder or( DBObject ... ors ){
return this; return this;
} }


/**
* Equivalent to an $and operand
* @param ands
* @return
*/
@SuppressWarnings("unchecked")
public QueryBuilder and( DBObject ... ands ){
List l = (List)_query.get( "$and" );
if ( l == null ){
l = new ArrayList();
_query.put( "$and" , l );
}
for ( DBObject o : ands )
l.add( o );
return this;
}

/** /**
* Creates a <code>DBObject</code> query to be used for the driver's find operations * Creates a <code>DBObject</code> query to be used for the driver's find operations
* @return Returns a DBObject query instance * @return Returns a DBObject query instance
Expand Down
16 changes: 16 additions & 0 deletions src/test/com/mongodb/QueryBuilderTest.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -291,6 +291,22 @@ public void testOr() {


assertEquals( 2 , c.find( q ).itcount() ); assertEquals( 2 , c.find( q ).itcount() );
} }

@Test
public void testAnd() {
DBCollection c = _testDB.getCollection( "and1" );
c.drop();
c.insert( new BasicDBObject( "a" , 1 ).append( "b" , 1) );
c.insert( new BasicDBObject( "b" , 1 ) );

DBObject q = QueryBuilder.start()
.and( new BasicDBObject( "a" , 1 ) ,
new BasicDBObject( "b" , 1 ) )
.get();

assertEquals( 1 , c.find( q ).itcount() );
}



@AfterClass @AfterClass
public static void tearDown() { public static void tearDown() {
Expand Down

0 comments on commit 94a5fda

Please sign in to comment.