File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 () {
You can’t perform that action at this time.
0 commit comments