File tree Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Expand file tree Collapse file tree 2 files changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -278,6 +278,23 @@ public QueryBuilder or( DBObject ... ors ){
278
278
return this ;
279
279
}
280
280
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
+
281
298
/**
282
299
* Creates a <code>DBObject</code> query to be used for the driver's find operations
283
300
* @return Returns a DBObject query instance
Original file line number Diff line number Diff line change @@ -291,6 +291,22 @@ public void testOr() {
291
291
292
292
assertEquals ( 2 , c .find ( q ).itcount () );
293
293
}
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
+
294
310
295
311
@ AfterClass
296
312
public static void tearDown () {
You can’t perform that action at this time.
0 commit comments