24
24
import org .springframework .data .geo .Metric ;
25
25
import org .springframework .data .geo .Metrics ;
26
26
import org .springframework .data .geo .Point ;
27
+ import org .springframework .data .mongodb .core .ReadConcernAware ;
28
+ import org .springframework .data .mongodb .core .ReadPreferenceAware ;
27
29
import org .springframework .data .mongodb .core .geo .GeoJsonPoint ;
28
30
import org .springframework .lang .Nullable ;
29
31
import org .springframework .util .Assert ;
30
32
import org .springframework .util .ObjectUtils ;
31
33
34
+ import com .mongodb .ReadConcern ;
35
+ import com .mongodb .ReadPreference ;
36
+
32
37
/**
33
38
* Builder class to build near-queries. <br />
34
39
* MongoDB {@code $geoNear} operator allows usage of a {@literal GeoJSON Point} or legacy coordinate pair. Though
171
176
* @author Christoph Strobl
172
177
* @author Mark Paluch
173
178
*/
174
- public final class NearQuery {
179
+ public final class NearQuery implements ReadConcernAware , ReadPreferenceAware {
175
180
176
181
private final Point point ;
177
182
private @ Nullable Query query ;
@@ -181,6 +186,8 @@ public final class NearQuery {
181
186
private boolean spherical ;
182
187
private @ Nullable Long limit ;
183
188
private @ Nullable Long skip ;
189
+ private @ Nullable ReadConcern readConcern ;
190
+ private @ Nullable ReadPreference readPreference ;
184
191
185
192
/**
186
193
* Creates a new {@link NearQuery}.
@@ -536,11 +543,6 @@ public NearQuery query(Query query) {
536
543
return this ;
537
544
}
538
545
539
- @ Nullable
540
- public Query getQuery () {
541
- return query ;
542
- }
543
-
544
546
/**
545
547
* @return the number of elements to skip.
546
548
*/
@@ -560,6 +562,74 @@ public Collation getCollation() {
560
562
return query != null ? query .getCollation ().orElse (null ) : null ;
561
563
}
562
564
565
+ /**
566
+ * Configures the query to use the given {@link ReadConcern} unless the underlying {@link #query(Query)}
567
+ * {@link Query#hasReadConcern() specifies} another one.
568
+ *
569
+ * @param readConcern must not be {@literal null}.
570
+ * @return this.
571
+ * @since 4.1
572
+ */
573
+ public NearQuery withReadConcern (ReadConcern readConcern ) {
574
+
575
+ Assert .notNull (readConcern , "ReadConcern must not be null" );
576
+ this .readConcern = readConcern ;
577
+ return this ;
578
+ }
579
+
580
+ /**
581
+ * Configures the query to use the given {@link ReadPreference} unless the underlying {@link #query(Query)}
582
+ * {@link Query#hasReadPreference() specifies} another one.
583
+ *
584
+ * @param readPreference must not be {@literal null}.
585
+ * @return this.
586
+ * @since 4.1
587
+ */
588
+ public NearQuery withReadPreference (ReadPreference readPreference ) {
589
+
590
+ Assert .notNull (readPreference , "ReadPreference must not be null" );
591
+ this .readPreference = readPreference ;
592
+ return this ;
593
+ }
594
+
595
+ /**
596
+ * Get the {@link ReadConcern} to use. Will return the underlying {@link #query(Query) queries}
597
+ * {@link Query#getReadConcern() ReadConcern} if present or the one defined on the {@link NearQuery#readConcern}
598
+ * itself.
599
+ *
600
+ * @return can be {@literal null} if none set.
601
+ * @since 4.1
602
+ * @see ReadConcernAware
603
+ */
604
+ @ Nullable
605
+ @ Override
606
+ public ReadConcern getReadConcern () {
607
+
608
+ if (query != null && query .hasReadConcern ()) {
609
+ return query .getReadConcern ();
610
+ }
611
+ return readConcern ;
612
+ }
613
+
614
+ /**
615
+ * Get the {@link ReadPreference} to use. Will return the underlying {@link #query(Query) queries}
616
+ * {@link Query#getReadPreference() ReadPreference} if present or the one defined on the
617
+ * {@link NearQuery#readPreference} itself.
618
+ *
619
+ * @return can be {@literal null} if none set.
620
+ * @since 4.1
621
+ * @see ReadPreferenceAware
622
+ */
623
+ @ Nullable
624
+ @ Override
625
+ public ReadPreference getReadPreference () {
626
+
627
+ if (query != null && query .hasReadPreference ()) {
628
+ return query .getReadPreference ();
629
+ }
630
+ return readPreference ;
631
+ }
632
+
563
633
/**
564
634
* Returns the {@link Document} built by the {@link NearQuery}.
565
635
*
0 commit comments