Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #55 from wfreeman/master

This adds $and support to QueryBuilder, along with a simple test.
  • Loading branch information...
commit 94a5fda917cd3e26bd462b2b5e67fc60e1df29bc 2 parents f4a2601 + 4585794
@bwmcadams bwmcadams authored
View
17 src/main/com/mongodb/QueryBuilder.java
@@ -279,6 +279,23 @@ public QueryBuilder or( DBObject ... ors ){
}
/**
+ * 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
* @throws RuntimeException if a key does not have a matching operand
View
16 src/test/com/mongodb/QueryBuilderTest.java
@@ -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() {
Please sign in to comment.
Something went wrong with that request. Please try again.