|
20 | 20 | import com.mongodb.annotations.Sealed;
|
21 | 21 | import com.mongodb.client.model.Aggregates;
|
22 | 22 | import com.mongodb.client.model.geojson.Point;
|
23 |
| - |
24 |
| -import java.util.UUID; |
25 |
| - |
| 23 | +import org.bson.BsonArray; |
26 | 24 | import org.bson.BsonBinary;
|
27 |
| -import org.bson.BsonNull; |
| 25 | +import org.bson.BsonBoolean; |
28 | 26 | import org.bson.BsonDocument;
|
| 27 | +import org.bson.BsonNull; |
29 | 28 | import org.bson.BsonType;
|
30 | 29 | import org.bson.Document;
|
31 | 30 | import org.bson.conversions.Bson;
|
| 31 | +import org.bson.types.ObjectId; |
32 | 32 |
|
33 | 33 | import java.time.Duration;
|
34 | 34 | import java.time.Instant;
|
35 | 35 | import java.util.Iterator;
|
36 |
| - |
37 |
| -import org.bson.types.ObjectId; |
| 36 | +import java.util.UUID; |
38 | 37 |
|
39 | 38 | import static com.mongodb.assertions.Assertions.isTrueArgument;
|
| 39 | +import static com.mongodb.assertions.Assertions.notNull; |
40 | 40 | import static com.mongodb.internal.Iterables.concat;
|
41 | 41 | import static com.mongodb.internal.client.model.Util.combineToBsonValue;
|
42 | 42 | import static java.util.Collections.singleton;
|
43 |
| -import static com.mongodb.assertions.Assertions.notNull; |
44 | 43 |
|
45 | 44 | /**
|
46 | 45 | * The core part of the {@link Aggregates#search(SearchOperator, SearchOptions) $search} pipeline stage of an aggregation pipeline.
|
@@ -300,6 +299,117 @@ static GeoNearSearchOperator near(final Point origin, final Number pivot, final
|
300 | 299 | .append("pivot", notNull("pivot", pivot)));
|
301 | 300 | }
|
302 | 301 |
|
| 302 | + /** |
| 303 | + * Returns a {@link SearchOperator} that searches for documents where the |
| 304 | + * value or array of values at a given path contains any of the specified values |
| 305 | + * |
| 306 | + * @param path The indexed field to be searched. |
| 307 | + * @param value The boolean value to search for. |
| 308 | + * @param values More fields to be searched. |
| 309 | + * @return The requested {@link SearchOperator}. |
| 310 | + * @mongodb.atlas.manual atlas-search/in/ in operator |
| 311 | + */ |
| 312 | + static InSearchOperator in(final FieldSearchPath path, final boolean value, final boolean... values) { |
| 313 | + notNull("values", values); |
| 314 | + BsonArray bsonArray = new BsonArray(); |
| 315 | + bsonArray.add(new BsonBoolean(value)); |
| 316 | + for (boolean v : values) { |
| 317 | + bsonArray.add(new BsonBoolean(v)); |
| 318 | + } |
| 319 | + return in(notNull("path", path), bsonArray); |
| 320 | + } |
| 321 | + |
| 322 | + /** |
| 323 | + * Returns a {@link SearchOperator} that searches for documents where the |
| 324 | + * value or array of values at a given path contains any of the specified values |
| 325 | + * |
| 326 | + * @param path The indexed field to be searched. |
| 327 | + * @param value The objectId value to search for. |
| 328 | + * @param values More fields to be searched. |
| 329 | + * @return The requested {@link SearchOperator}. |
| 330 | + * @mongodb.atlas.manual atlas-search/in/ in operator |
| 331 | + */ |
| 332 | + static InSearchOperator in(final FieldSearchPath path, final ObjectId value, final ObjectId... values) { |
| 333 | + return in(notNull("path", path), concat(notNull("value", value), values)); |
| 334 | + } |
| 335 | + |
| 336 | + /** |
| 337 | + * Returns a {@link SearchOperator} that searches for documents where the |
| 338 | + * value or array of values at a given path contains any of the specified values |
| 339 | + * |
| 340 | + * @param path The indexed field to be searched. |
| 341 | + * @param value The number value to search for. |
| 342 | + * @param values More fields to be searched. |
| 343 | + * @return The requested {@link SearchOperator}. |
| 344 | + * @mongodb.atlas.manual atlas-search/in/ in operator |
| 345 | + */ |
| 346 | + static InSearchOperator in(final FieldSearchPath path, final Number value, final Number... values) { |
| 347 | + return in(notNull("path", path), concat(notNull("value", value), values)); |
| 348 | + } |
| 349 | + |
| 350 | + /** |
| 351 | + * Returns a {@link SearchOperator} that searches for documents where the |
| 352 | + * value or array of values at a given path contains any of the specified values |
| 353 | + * |
| 354 | + * @param path The indexed field to be searched. |
| 355 | + * @param value The instant date value to search for. |
| 356 | + * @param values More fields to be searched. |
| 357 | + * @return The requested {@link SearchOperator}. |
| 358 | + * @mongodb.atlas.manual atlas-search/in/ in operator |
| 359 | + */ |
| 360 | + static InSearchOperator in(final FieldSearchPath path, final Instant value, final Instant... values) { |
| 361 | + return in(notNull("path", path), concat(notNull("value", value), values)); |
| 362 | + } |
| 363 | + |
| 364 | + /** |
| 365 | + * Returns a {@link SearchOperator} that searches for documents where the |
| 366 | + * value or array of values at a given path contains any of the specified values |
| 367 | + * |
| 368 | + * @param path The indexed field to be searched. |
| 369 | + * @param value The uuid value to search for. |
| 370 | + * @param values More fields to be searched. |
| 371 | + * @return The requested {@link SearchOperator}. |
| 372 | + * @mongodb.atlas.manual atlas-search/in/ in operator |
| 373 | + */ |
| 374 | + static InSearchOperator in(final FieldSearchPath path, final UUID value, final UUID... values) { |
| 375 | + return in(notNull("path", path), concat(notNull("value", value), values)); |
| 376 | + } |
| 377 | + |
| 378 | + /** |
| 379 | + * Returns a {@link SearchOperator} that searches for documents where the |
| 380 | + * value or array of values at a given path contains any of the specified values |
| 381 | + * |
| 382 | + * @param path The indexed field to be searched. |
| 383 | + * @param value The string value to search for. |
| 384 | + * @param values More fields to be searched. |
| 385 | + * @return The requested {@link SearchOperator}. |
| 386 | + * @mongodb.atlas.manual atlas-search/in/ in operator |
| 387 | + */ |
| 388 | + static InSearchOperator in(final FieldSearchPath path, final String value, final String... values) { |
| 389 | + return in(notNull("path", path), concat(notNull("value", value), values)); |
| 390 | + } |
| 391 | + |
| 392 | + /** |
| 393 | + * Returns a {@link SearchOperator} that searches for documents where the |
| 394 | + * value or array of values at a given path contains any of the specified values |
| 395 | + * |
| 396 | + * @param path The indexed field to be searched. |
| 397 | + * @param values The non-empty values to search for. Value can be either a single value or an array of values of only one of the supported BSON types and can't be a mix of different types. |
| 398 | + * @param <T> the type of elements in {@code values}. |
| 399 | + * @return The requested {@link SearchOperator}. |
| 400 | + * @mongodb.atlas.manual atlas-search/in/ in operator |
| 401 | + */ |
| 402 | + static <T> InSearchOperator in(final FieldSearchPath path, final Iterable<? extends T> values) { |
| 403 | + notNull("path", path); |
| 404 | + Iterator<? extends T> valueIterator = notNull("values", values).iterator(); |
| 405 | + isTrueArgument("values must not be empty", valueIterator.hasNext()); |
| 406 | + T firstValue = valueIterator.next(); |
| 407 | + boolean hasMore = valueIterator.hasNext(); |
| 408 | + return new SearchConstructibleBsonElement("in", new Document() |
| 409 | + .append("path", path.toValue()) |
| 410 | + .append("value", hasMore ? values : firstValue)); |
| 411 | + } |
| 412 | + |
303 | 413 | /**
|
304 | 414 | * Returns a {@link SearchOperator} that searches for documents where a field matches the specified value.
|
305 | 415 | *
|
|
0 commit comments