Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/com/mongodb/QueryBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,23 @@ public QueryBuilder or( DBObject ... ors ){
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
* @return Returns a DBObject query instance
Expand Down
16 changes: 16 additions & 0 deletions src/test/com/mongodb/QueryBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,22 @@ public void testOr() {

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
public static void tearDown() {
Expand Down