@@ -291,7 +291,11 @@ public WriteResult remove( DBObject o )
291
291
/**
292
292
* Finds objects
293
293
*/
294
- abstract Iterator <DBObject > __find ( DBObject ref , DBObject fields , int numToSkip , int batchSize , int limit , int options ) throws MongoException ;
294
+ Iterator <DBObject > __find ( DBObject ref , DBObject fields , int numToSkip , int batchSize , int limit , int options ) throws MongoException {
295
+ return __find ( ref , fields , numToSkip , batchSize , limit , options , _readPref );
296
+ }
297
+
298
+ abstract Iterator <DBObject > __find ( DBObject ref , DBObject fields , int numToSkip , int batchSize , int limit , int options , ReadPreference readPref ) throws MongoException ;
295
299
296
300
/**
297
301
* Calls {@link DBCollection#find(com.mongodb.DBObject, com.mongodb.DBObject, int, int)} and applies the query options
@@ -340,7 +344,20 @@ public final DBCursor find( DBObject query , DBObject fields , int numToSkip , i
340
344
*/
341
345
public final DBObject findOne ( Object obj )
342
346
throws MongoException {
343
- return findOne (obj , null );
347
+ return findOne (obj , null , _readPref );
348
+ }
349
+
350
+ /**
351
+ * Finds an object by its id.
352
+ * This compares the passed in value to the _id field of the document
353
+ *
354
+ * @param obj any valid object
355
+ * @return the object, if found, otherwise <code>null</code>
356
+ * @throws MongoException
357
+ */
358
+ public final DBObject findOne ( Object obj , ReadPreference readPref )
359
+ throws MongoException {
360
+ return findOne (obj , null , readPref );
344
361
}
345
362
346
363
/**
@@ -353,7 +370,20 @@ public final DBObject findOne( Object obj )
353
370
* @dochub find
354
371
*/
355
372
public final DBObject findOne ( Object obj , DBObject fields ) {
356
- Iterator <DBObject > iterator = __find (new BasicDBObject ("_id" , obj ), fields , 0 , -1 , 0 , getOptions () );
373
+ return findOne ( obj , fields , _readPref );
374
+ }
375
+
376
+ /**
377
+ * Finds an object by its id.
378
+ * This compares the passed in value to the _id field of the document
379
+ *
380
+ * @param obj any valid object
381
+ * @param fields fields to return
382
+ * @return the object, if found, otherwise <code>null</code>
383
+ * @dochub find
384
+ */
385
+ public final DBObject findOne ( Object obj , DBObject fields , ReadPreference readPref ) {
386
+ Iterator <DBObject > iterator = __find (new BasicDBObject ("_id" , obj ), fields , 0 , -1 , 0 , getOptions (), readPref );
357
387
return (iterator != null ? iterator .next () : null );
358
388
}
359
389
@@ -578,7 +608,18 @@ public void setHintFields( List<DBObject> lst ){
578
608
* @dochub find
579
609
*/
580
610
public final DBCursor find ( DBObject ref ){
581
- return new DBCursor ( this , ref , null );
611
+ return find ( ref , _readPref );
612
+ }
613
+
614
+ /**
615
+ * Queries for an object in this collection.
616
+ * @param ref object for which to search
617
+ * @param preference Read Preference for this write
618
+ * @return an iterator over the results
619
+ * @dochub find
620
+ */
621
+ public final DBCursor find ( DBObject ref , ReadPreference preference ){
622
+ return new DBCursor ( this , ref , null , preference );
582
623
}
583
624
584
625
/**
@@ -605,7 +646,34 @@ public final DBCursor find( DBObject ref ){
605
646
* @dochub find
606
647
*/
607
648
public final DBCursor find ( DBObject ref , DBObject keys ){
608
- return new DBCursor ( this , ref , keys );
649
+ return find ( ref , keys , _readPref );
650
+ }
651
+
652
+ /**
653
+ * Queries for an object in this collection.
654
+ *
655
+ * <p>
656
+ * An empty DBObject will match every document in the collection.
657
+ * Regardless of fields specified, the _id fields are always returned.
658
+ * </p>
659
+ * <p>
660
+ * An example that returns the "x" and "_id" fields for every document
661
+ * in the collection that has an "x" field:
662
+ * </p>
663
+ * <blockquote><pre>
664
+ * BasicDBObject keys = new BasicDBObject();
665
+ * keys.put("x", 1);
666
+ *
667
+ * DBCursor cursor = collection.find(new BasicDBObject(), keys);
668
+ * </pre></blockquote>
669
+ *
670
+ * @param ref object for which to search
671
+ * @param keys fields to return
672
+ * @return a cursor to iterate over results
673
+ * @dochub find
674
+ */
675
+ public final DBCursor find ( DBObject ref , DBObject keys , ReadPreference readPref ){
676
+ return new DBCursor ( this , ref , keys , readPref );
609
677
}
610
678
611
679
/**
@@ -614,7 +682,16 @@ public final DBCursor find( DBObject ref , DBObject keys ){
614
682
* @dochub find
615
683
*/
616
684
public final DBCursor find (){
617
- return new DBCursor ( this , new BasicDBObject (), null );
685
+ return find ( _readPref );
686
+ }
687
+
688
+ /**
689
+ * Queries for all objects in this collection.
690
+ * @return a cursor which will iterate over every object
691
+ * @dochub find
692
+ */
693
+ public final DBCursor find ( ReadPreference readPref ){
694
+ return new DBCursor ( this , new BasicDBObject (), null , readPref );
618
695
}
619
696
620
697
/**
@@ -627,6 +704,16 @@ public final DBObject findOne()
627
704
return findOne ( new BasicDBObject () );
628
705
}
629
706
707
+ /**
708
+ * Returns a single object from this collection.
709
+ * @return the object found, or <code>null</code> if the collection is empty
710
+ * @throws MongoException
711
+ */
712
+ public final DBObject findOne ( ReadPreference readPref )
713
+ throws MongoException {
714
+ return findOne ( new BasicDBObject (), readPref );
715
+ }
716
+
630
717
/**
631
718
* Returns a single object from this collection matching the query.
632
719
* @param o the query object
@@ -635,7 +722,18 @@ public final DBObject findOne()
635
722
*/
636
723
public final DBObject findOne ( DBObject o )
637
724
throws MongoException {
638
- return findOne (o , null );
725
+ return findOne ( o , null , _readPref );
726
+ }
727
+
728
+ /**
729
+ * Returns a single object from this collection matching the query.
730
+ * @param o the query object
731
+ * @return the object found, or <code>null</code> if no such object exists
732
+ * @throws MongoException
733
+ */
734
+ public final DBObject findOne ( DBObject o , ReadPreference readPref )
735
+ throws MongoException {
736
+ return findOne ( o , null , readPref );
639
737
}
640
738
641
739
/**
@@ -646,14 +744,25 @@ public final DBObject findOne( DBObject o )
646
744
* @dochub find
647
745
*/
648
746
public final DBObject findOne ( DBObject o , DBObject fields ) {
649
- Iterator <DBObject > i = __find ( o , fields , 0 , -1 , 0 , getOptions () );
747
+ return findOne ( o , fields , _readPref );
748
+ }
749
+ /**
750
+ * Returns a single object from this collection matching the query.
751
+ * @param o the query object
752
+ * @param fields fields to return
753
+ * @return the object found, or <code>null</code> if no such object exists
754
+ * @dochub find
755
+ */
756
+ public final DBObject findOne ( DBObject o , DBObject fields , ReadPreference readPref ) {
757
+ Iterator <DBObject > i = __find ( o , fields , 0 , -1 , 0 , getOptions (), readPref );
650
758
DBObject obj = (i == null ? null : i .next ());
651
759
if ( obj != null && ( fields != null && fields .keySet ().size () > 0 ) ){
652
760
obj .markAsPartialObject ();
653
761
}
654
762
return obj ;
655
763
}
656
764
765
+
657
766
/**
658
767
* calls {@link DBCollection#apply(com.mongodb.DBObject, boolean)} with ensureID=true
659
768
* @param o <code>DBObject</code> to which to add fields
0 commit comments