Skip to content

Commit 94a5fda

Browse files
author
Brendan W. McAdams
committed
Merge pull request mongodb#55 from wfreeman/master
This adds $and support to QueryBuilder, along with a simple test.
2 parents f4a2601 + 4585794 commit 94a5fda

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/main/com/mongodb/QueryBuilder.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,23 @@ public QueryBuilder or( DBObject ... ors ){
278278
return this;
279279
}
280280

281+
/**
282+
* Equivalent to an $and operand
283+
* @param ands
284+
* @return
285+
*/
286+
@SuppressWarnings("unchecked")
287+
public QueryBuilder and( DBObject ... ands ){
288+
List l = (List)_query.get( "$and" );
289+
if ( l == null ){
290+
l = new ArrayList();
291+
_query.put( "$and" , l );
292+
}
293+
for ( DBObject o : ands )
294+
l.add( o );
295+
return this;
296+
}
297+
281298
/**
282299
* Creates a <code>DBObject</code> query to be used for the driver's find operations
283300
* @return Returns a DBObject query instance

src/test/com/mongodb/QueryBuilderTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,22 @@ public void testOr() {
291291

292292
assertEquals( 2 , c.find( q ).itcount() );
293293
}
294+
295+
@Test
296+
public void testAnd() {
297+
DBCollection c = _testDB.getCollection( "and1" );
298+
c.drop();
299+
c.insert( new BasicDBObject( "a" , 1 ).append( "b" , 1) );
300+
c.insert( new BasicDBObject( "b" , 1 ) );
301+
302+
DBObject q = QueryBuilder.start()
303+
.and( new BasicDBObject( "a" , 1 ) ,
304+
new BasicDBObject( "b" , 1 ) )
305+
.get();
306+
307+
assertEquals( 1 , c.find( q ).itcount() );
308+
}
309+
294310

295311
@AfterClass
296312
public static void tearDown() {

0 commit comments

Comments
 (0)