From c248c7342244444dc80cb6d36c009d5bb1db66e0 Mon Sep 17 00:00:00 2001 From: Joy Kim Date: Mon, 13 Jan 2025 11:07:30 -0500 Subject: [PATCH 01/12] wip --- .../client/model/search/InSearchOperator.java | 44 ++++++ .../SearchConstructibleBsonElement.java | 2 +- .../client/model/search/SearchOperator.java | 137 ++++++++++++++++++ .../AggregatesSearchIntegrationTest.java | 7 +- .../model/search/SearchOperatorTest.java | 104 +++++++++++++ .../scala/model/search/SearchOperator.scala | 108 ++++++++++++++ .../mongodb/scala/model/search/package.scala | 7 + 7 files changed, 407 insertions(+), 2 deletions(-) create mode 100644 driver-core/src/main/com/mongodb/client/model/search/InSearchOperator.java diff --git a/driver-core/src/main/com/mongodb/client/model/search/InSearchOperator.java b/driver-core/src/main/com/mongodb/client/model/search/InSearchOperator.java new file mode 100644 index 00000000000..73133237378 --- /dev/null +++ b/driver-core/src/main/com/mongodb/client/model/search/InSearchOperator.java @@ -0,0 +1,44 @@ +/* + * Copyright 2008-present MongoDB, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mongodb.client.model.search; + +import com.mongodb.annotations.Beta; +import com.mongodb.annotations.Reason; +import com.mongodb.annotations.Sealed; + +import java.util.UUID; + +import java.time.Instant; + +import org.bson.types.ObjectId; + +/** + * @see SearchOperator#in(FieldSearchPath, Boolean, Boolean...) + * @see SearchOperator#in(FieldSearchPath, ObjectId, ObjectId...) + * @see SearchOperator#in(FieldSearchPath, Number, Number...) + * @see SearchOperator#in(FieldSearchPath, Instant, Instant...) + * @see SearchOperator#in(FieldSearchPath, UUID, UUID...) + * @see SearchOperator#in(FieldSearchPath, String, String...) + * @see SearchOperator#inNull(FieldSearchPath) + * @see SearchOperator#in(FieldSearchPath, Iterable) + * @since 5.3 + */ +@Sealed +@Beta(Reason.CLIENT) +public interface InSearchOperator extends SearchOperator { + @Override + InSearchOperator score(SearchScore modifier); +} diff --git a/driver-core/src/main/com/mongodb/client/model/search/SearchConstructibleBsonElement.java b/driver-core/src/main/com/mongodb/client/model/search/SearchConstructibleBsonElement.java index 8f0b1e510c5..c293e683e5e 100644 --- a/driver-core/src/main/com/mongodb/client/model/search/SearchConstructibleBsonElement.java +++ b/driver-core/src/main/com/mongodb/client/model/search/SearchConstructibleBsonElement.java @@ -31,7 +31,7 @@ final class SearchConstructibleBsonElement extends AbstractConstructibleBsonElement implements MustCompoundSearchOperator, MustNotCompoundSearchOperator, ShouldCompoundSearchOperator, FilterCompoundSearchOperator, ExistsSearchOperator, TextSearchOperator, AutocompleteSearchOperator, - NumberNearSearchOperator, DateNearSearchOperator, GeoNearSearchOperator, + NumberNearSearchOperator, DateNearSearchOperator, GeoNearSearchOperator, InSearchOperator, ValueBoostSearchScore, PathBoostSearchScore, ConstantSearchScore, FunctionSearchScore, GaussSearchScoreExpression, PathSearchScoreExpression, FacetSearchCollector, diff --git a/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java b/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java index 9234db91c51..43bff0e06d7 100644 --- a/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java +++ b/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java @@ -20,6 +20,13 @@ import com.mongodb.annotations.Sealed; import com.mongodb.client.model.Aggregates; import com.mongodb.client.model.geojson.Point; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import org.bson.BsonBinary; +import org.bson.BsonNull; import org.bson.BsonType; import org.bson.Document; import org.bson.conversions.Bson; @@ -28,6 +35,8 @@ import java.time.Instant; import java.util.Iterator; +import org.bson.types.ObjectId; + import static com.mongodb.assertions.Assertions.isTrueArgument; import static com.mongodb.internal.Iterables.concat; import static com.mongodb.internal.client.model.Util.combineToBsonValue; @@ -292,6 +301,134 @@ static GeoNearSearchOperator near(final Point origin, final Number pivot, final .append("pivot", notNull("pivot", pivot))); } + /** + * Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of + * the field equals any value in the specified array. + * + * @param path The indexed field to be searched. + * @param value The boolean value to search for. + * @param values More fields to be searched. + * @return The requested {@link SearchOperator}. + * @mongodb.atlas.manual atlas-search/in/ in operator + */ + static InSearchOperator in(final FieldSearchPath path, final Boolean value, final Boolean... values) { + return in(notNull("path", path), concat(notNull("value", value), values)); + } + + /** + * Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of + * the field equals any value in the specified array. + * + * @param path The indexed field to be searched. + * @param value The objectId value to search for. + * @param values More fields to be searched. + * @return The requested {@link SearchOperator}. + * @mongodb.atlas.manual atlas-search/in/ in operator + */ + static InSearchOperator in(final FieldSearchPath path, final ObjectId value, final ObjectId... values) { + return in(notNull("path", path), concat(notNull("value", value), values)); + } + + /** + * Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of + * the field equals any value in the specified array. + * + * @param path The indexed field to be searched. + * @param value The number value to search for. + * @param values More fields to be searched. + * @return The requested {@link SearchOperator}. + * @mongodb.atlas.manual atlas-search/in/ in operator + */ + static InSearchOperator in(final FieldSearchPath path, final Number value, final Number... values) { + return in(notNull("path", path), concat(notNull("value", value), values)); + } + + /** + * Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of + * the field equals any value in the specified array. + * + * @param path The indexed field to be searched. + * @param value The instant date value to search for. + * @param values More fields to be searched. + * @return The requested {@link SearchOperator}. + * @mongodb.atlas.manual atlas-search/in/ in operator + */ + static InSearchOperator in(final FieldSearchPath path, final Instant value, final Instant... values) { + return in(notNull("path", path), concat(notNull("value", value), values)); + } + + /** + * Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of + * the field equals any value in the specified array. + * + * @param path The indexed field to be searched. + * @param value The uuid value to search for. + * @param values More fields to be searched. + * @return The requested {@link SearchOperator}. + * @mongodb.atlas.manual atlas-search/in/ in operator + */ + static InSearchOperator in(final FieldSearchPath path, final UUID value, final UUID... values) { + List bsonValues = new ArrayList<>(); + bsonValues.add(new BsonBinary(value)); + if (values != null) { + for (UUID uuid : values) { + bsonValues.add(new BsonBinary(uuid)); + } + } + return in(notNull("path", path), notNull("value", bsonValues)); + } + + /** + * Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of + * the field equals any value in the specified array. + * + * @param path The indexed field to be searched. + * @param value The string value to search for. + * @param values More fields to be searched. + * @return The requested {@link SearchOperator}. + * @mongodb.atlas.manual atlas-search/in/ in operator + */ + static InSearchOperator in(final FieldSearchPath path, final String value, final String... values) { + return in(notNull("path", path), concat(notNull("value", value), values)); + } + + /** + * Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of + * the field equals null. + * + * @param path The indexed field to be searched. + * @return The requested {@link SearchOperator}. + * @mongodb.atlas.manual atlas-search/in/ in operator + */ + static InSearchOperator inNull(final FieldSearchPath path) { + return new SearchConstructibleBsonElement("in", new Document("path", notNull("path", path).toValue()) + .append("value", BsonNull.VALUE)); + } + + /** + * Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of + * * the field equals any value in the specified array. + * + * @param path The indexed field to be searched. + * @param values The non-empty values to search for. + * @return The requested {@link SearchOperator}. + * @mongodb.atlas.manual atlas-search/in/ in operator + */ + static InSearchOperator in(final FieldSearchPath path, final Iterable values) { + Iterator valueIterator = (Iterator) notNull("values", values).iterator(); + isTrueArgument("values must not be empty", valueIterator.hasNext()); + T firstValue = valueIterator.next(); + + Iterator typeIterator = (Iterator) notNull("values", values).iterator(); + while (typeIterator.hasNext()) { + Object element = typeIterator.next(); + isTrueArgument("values must be of same type", firstValue.getClass().isInstance(element)); + } + + return new SearchConstructibleBsonElement("in", new Document("path", notNull("path", path).toValue()) + .append("value", valueIterator.hasNext() ? values : firstValue)); + } + /** * Creates a {@link SearchOperator} from a {@link Bson} in situations when there is no builder method that better satisfies your needs. * This method cannot be used to validate the syntax. diff --git a/driver-core/src/test/functional/com/mongodb/client/model/search/AggregatesSearchIntegrationTest.java b/driver-core/src/test/functional/com/mongodb/client/model/search/AggregatesSearchIntegrationTest.java index 29de80dda32..93cd1e80f49 100644 --- a/driver-core/src/test/functional/com/mongodb/client/model/search/AggregatesSearchIntegrationTest.java +++ b/driver-core/src/test/functional/com/mongodb/client/model/search/AggregatesSearchIntegrationTest.java @@ -80,6 +80,8 @@ import static com.mongodb.client.model.search.SearchOperator.compound; import static com.mongodb.client.model.search.SearchOperator.dateRange; import static com.mongodb.client.model.search.SearchOperator.exists; +import static com.mongodb.client.model.search.SearchOperator.in; +import static com.mongodb.client.model.search.SearchOperator.inNull; import static com.mongodb.client.model.search.SearchOperator.near; import static com.mongodb.client.model.search.SearchOperator.numberRange; import static com.mongodb.client.model.search.SearchOperator.text; @@ -608,7 +610,10 @@ private static Stream searchAndSearchMetaArgs() { dateRange(fieldPath("fieldName6")) .lte(Instant.ofEpochMilli(1)), near(0, 1.5, fieldPath("fieldName7"), fieldPath("fieldName8")), - near(Instant.ofEpochMilli(1), Duration.ofMillis(3), fieldPath("fieldName9")) + near(Instant.ofEpochMilli(1), Duration.ofMillis(3), fieldPath("fieldName9")), + in(fieldPath("fieldName10"), true), + in(fieldPath("fieldName11"), "term4", "term5"), + inNull(fieldPath("fieldName12")) )) .minimumShouldMatch(1) .mustNot(singleton( diff --git a/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java b/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java index c0ea645fb73..504a881d954 100644 --- a/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java +++ b/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java @@ -18,14 +18,22 @@ import com.mongodb.MongoClientSettings; import com.mongodb.client.model.geojson.Point; import com.mongodb.client.model.geojson.Position; + +import java.util.UUID; + import org.bson.BsonArray; +import org.bson.BsonBinary; +import org.bson.BsonBoolean; import org.bson.BsonDateTime; import org.bson.BsonDocument; import org.bson.BsonDouble; import org.bson.BsonInt32; import org.bson.BsonInt64; +import org.bson.BsonNull; +import org.bson.BsonObjectId; import org.bson.BsonString; import org.bson.Document; +import org.bson.types.ObjectId; import org.junit.jupiter.api.Test; import java.time.Duration; @@ -581,6 +589,102 @@ void near() { ); } + @Test + void in() { + ObjectId objectId = new ObjectId(); + UUID uuid = UUID.randomUUID(); + assertAll( + () -> assertThrows(IllegalArgumentException.class, () -> + // paths must not be empty + SearchOperator.in(null, true) + ), + () -> assertThrows(IllegalArgumentException.class, () -> + // values should be of one supported BSON types and can't be a mix of different types + SearchOperator.in(fieldPath("fieldName1"), asList(true, "value")) + ), + () -> assertEquals( + new BsonDocument("in", + new BsonDocument("path", fieldPath("fieldName1").toBsonValue()) + .append("value", new BsonBoolean(true)) + ), + SearchOperator.in(fieldPath("fieldName1"), true) + .toBsonDocument() + ), + () -> assertEquals( + new BsonDocument("in", + new BsonDocument("path", fieldPath("fieldName1").toBsonValue()) + .append("value", new BsonArray(asList(new BsonBoolean(true), new BsonBoolean(false)))) + ), + SearchOperator.in(fieldPath("fieldName1"), asList(true, false)) + .toBsonDocument() + ), + () -> assertEquals( + new BsonDocument("in", + new BsonDocument("path", fieldPath("fieldName1").toBsonValue()) + .append("value", new BsonObjectId(objectId)) + ), + SearchOperator.in(fieldPath("fieldName1"), objectId) + .toBsonDocument() + ), + () -> assertEquals( + new BsonDocument("in", + new BsonDocument("path", fieldPath("fieldName1").toBsonValue()) + .append("value", new BsonInt32(1)) + ), + SearchOperator.in(fieldPath("fieldName1"), 1) + .toBsonDocument() + ), + () -> assertEquals( + new BsonDocument("in", + new BsonDocument("path", fieldPath("fieldName1").toBsonValue()) + .append("value", new BsonInt64(Long.MAX_VALUE)) + ), + SearchOperator.in(fieldPath("fieldName1"), Long.MAX_VALUE) + .toBsonDocument() + ), + () -> assertEquals( + new BsonDocument("in", + new BsonDocument("path", fieldPath("fieldName1").toBsonValue()) + .append("value", new BsonDouble(Double.MAX_VALUE)) + ), + SearchOperator.in(fieldPath("fieldName1"), Double.MAX_VALUE) + .toBsonDocument() + ), + () -> assertEquals( + new BsonDocument("in", + new BsonDocument("path", fieldPath("fieldName1").toBsonValue()) + .append("value", new BsonDateTime(Instant.EPOCH.toEpochMilli())) + ), + SearchOperator.in(fieldPath("fieldName1"), Instant.EPOCH) + .toBsonDocument() + ), + () -> assertEquals( + new BsonDocument("in", + new BsonDocument("path", fieldPath("fieldName1").toBsonValue()) + .append("value", new BsonBinary(uuid)) + ), + SearchOperator.in(fieldPath("fieldName1"), uuid) + .toBsonDocument() + ), + () -> assertEquals( + new BsonDocument("in", + new BsonDocument("path", fieldPath("fieldName1").toBsonValue()) + .append("value", new BsonString("value")) + ), + SearchOperator.in(fieldPath("fieldName1"), "value") + .toBsonDocument() + ), + () -> assertEquals( + new BsonDocument("in", + new BsonDocument("path", fieldPath("fieldName1").toBsonValue()) + .append("value", BsonNull.VALUE) + ), + SearchOperator.inNull(fieldPath("fieldName1")) + .toBsonDocument() + ) + ); + } + private static SearchOperator docExamplePredefined() { return SearchOperator.exists( fieldPath("fieldName")); diff --git a/driver-scala/src/main/scala/org/mongodb/scala/model/search/SearchOperator.scala b/driver-scala/src/main/scala/org/mongodb/scala/model/search/SearchOperator.scala index 90f27092ebc..ee6521ea193 100644 --- a/driver-scala/src/main/scala/org/mongodb/scala/model/search/SearchOperator.scala +++ b/driver-scala/src/main/scala/org/mongodb/scala/model/search/SearchOperator.scala @@ -17,10 +17,18 @@ package org.mongodb.scala.model.search import com.mongodb.annotations.{ Beta, Reason } import com.mongodb.client.model.search.{ SearchOperator => JSearchOperator } + +import java.lang.Boolean + +import java.util.UUID; + import org.mongodb.scala.bson.conversions.Bson import org.mongodb.scala.model.geojson.Point import java.time.{ Duration, Instant } + +import org.bson.types.ObjectId; + import collection.JavaConverters._ /** @@ -228,6 +236,106 @@ object SearchOperator { def near(origin: Point, pivot: Number, paths: Iterable[_ <: FieldSearchPath]): GeoNearSearchOperator = JSearchOperator.near(origin, pivot, paths.asJava) + /** + * Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of + * the field equals any value in the specified array. + * + * @param path The indexed field to be searched. + * @param value The boolean value to search for. + * @param values More fields to be searched. + * @return The requested `SearchOperator`. + * @see [[https://www.mongodb.com/docs/atlas/atlas-search/in/ in operator]] + */ + def in(path: FieldSearchPath, value: Boolean, values: Boolean*): InSearchOperator = + JSearchOperator.in(path, value, values: _*) + + /** + * Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of + * the field equals any value in the specified array. + * + * @param path The indexed field to be searched. + * @param value The objectId value to search for. + * @param values More fields to be searched. + * @return The requested `SearchOperator`. + * @see [[https://www.mongodb.com/docs/atlas/atlas-search/in/ in operator]] + */ + def in(path: FieldSearchPath, value: ObjectId, values: ObjectId*): InSearchOperator = + JSearchOperator.in(path, value, values: _*) + + /** + * Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of + * the field equals any value in the specified array. + * + * @param path The indexed field to be searched. + * @param value The number value to search for. + * @param values More fields to be searched. + * @return The requested `SearchOperator`. + * @see [[https://www.mongodb.com/docs/atlas/atlas-search/in/ in operator]] + */ + def in(path: FieldSearchPath, value: Number, values: Number*): InSearchOperator = + JSearchOperator.in(path, value, values: _*) + + /** + * Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of + * the field equals any value in the specified array. + * + * @param path The indexed field to be searched. + * @param value The instant date value to search for. + * @param values More fields to be searched. + * @return The requested `SearchOperator`. + * @see [[https://www.mongodb.com/docs/atlas/atlas-search/in/ in operator]] + */ + def in(path: FieldSearchPath, value: Instant, values: Instant*): InSearchOperator = + JSearchOperator.in(path, value, values: _*) + + /** + * Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of + * the field equals any value in the specified array. + * + * @param path The indexed field to be searched. + * @param value The uuid value to search for. + * @param values More fields to be searched. + * @return The requested `SearchOperator`. + * @see [[https://www.mongodb.com/docs/atlas/atlas-search/in/ in operator]] + */ + def in(path: FieldSearchPath, value: UUID, values: UUID*): InSearchOperator = + JSearchOperator.in(path, value, values: _*) + + /** + * Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of + * the field equals any value in the specified array. + * + * @param path The indexed field to be searched. + * @param value The string value to search for. + * @param values More fields to be searched. + * @return The requested `SearchOperator`. + * @see [[https://www.mongodb.com/docs/atlas/atlas-search/in/ in operator]] + */ + def in(path: FieldSearchPath, value: String, values: String*): InSearchOperator = + JSearchOperator.in(path, value, values: _*) + + /** + * Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of + * the field equals null. + * + * @param path The indexed field to be searched. + * @return The requested `SearchOperator`. + * @see [[https://www.mongodb.com/docs/atlas/atlas-search/in/ in operator]] + */ + def inNull(path: FieldSearchPath): InSearchOperator = JSearchOperator.inNull(path) + + /** + * Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of + * the field equals any value in the specified array. + * + * @param path The indexed field to be searched. + * @param values The non-empty values to search for. + * @return The requested `SearchOperator`. + * @see [[https://www.mongodb.com/docs/atlas/atlas-search/in/ in operator]] + */ + def in[T](path: FieldSearchPath, values: Iterable[_ <: T]): InSearchOperator = + JSearchOperator.in(path, values.asJava) + /** * Creates a `SearchOperator` from a `Bson` in situations when there is no builder method that better satisfies your needs. * This method cannot be used to validate the syntax. diff --git a/driver-scala/src/main/scala/org/mongodb/scala/model/search/package.scala b/driver-scala/src/main/scala/org/mongodb/scala/model/search/package.scala index 557060324cd..88fc86cb4fe 100644 --- a/driver-scala/src/main/scala/org/mongodb/scala/model/search/package.scala +++ b/driver-scala/src/main/scala/org/mongodb/scala/model/search/package.scala @@ -182,6 +182,13 @@ package object search { @Beta(Array(Reason.CLIENT)) type GeoNearSearchOperator = com.mongodb.client.model.search.GeoNearSearchOperator + /** + * @see `SearchOperator.in` + */ + @Sealed + @Beta(Array(Reason.CLIENT)) + type InSearchOperator = com.mongodb.client.model.search.InSearchOperator + /** * Fuzzy search options that may be used with some [[SearchOperator]]s. * From b6c0b98694cfef236b9ef049fad2edac22226a32 Mon Sep 17 00:00:00 2001 From: Joy Kim Date: Mon, 13 Jan 2025 13:40:26 -0500 Subject: [PATCH 02/12] update with Evergreen test --- .../client/model/search/InSearchOperator.java | 1 - .../client/model/search/SearchOperator.java | 14 -------------- .../search/AggregatesSearchIntegrationTest.java | 4 +--- .../client/model/search/SearchOperatorTest.java | 9 --------- .../scala/model/search/SearchOperator.scala | 10 ---------- 5 files changed, 1 insertion(+), 37 deletions(-) diff --git a/driver-core/src/main/com/mongodb/client/model/search/InSearchOperator.java b/driver-core/src/main/com/mongodb/client/model/search/InSearchOperator.java index 73133237378..4540db60f1f 100644 --- a/driver-core/src/main/com/mongodb/client/model/search/InSearchOperator.java +++ b/driver-core/src/main/com/mongodb/client/model/search/InSearchOperator.java @@ -32,7 +32,6 @@ * @see SearchOperator#in(FieldSearchPath, Instant, Instant...) * @see SearchOperator#in(FieldSearchPath, UUID, UUID...) * @see SearchOperator#in(FieldSearchPath, String, String...) - * @see SearchOperator#inNull(FieldSearchPath) * @see SearchOperator#in(FieldSearchPath, Iterable) * @since 5.3 */ diff --git a/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java b/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java index 43bff0e06d7..4267afbe2af 100644 --- a/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java +++ b/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java @@ -26,7 +26,6 @@ import java.util.UUID; import org.bson.BsonBinary; -import org.bson.BsonNull; import org.bson.BsonType; import org.bson.Document; import org.bson.conversions.Bson; @@ -392,19 +391,6 @@ static InSearchOperator in(final FieldSearchPath path, final String value, final return in(notNull("path", path), concat(notNull("value", value), values)); } - /** - * Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of - * the field equals null. - * - * @param path The indexed field to be searched. - * @return The requested {@link SearchOperator}. - * @mongodb.atlas.manual atlas-search/in/ in operator - */ - static InSearchOperator inNull(final FieldSearchPath path) { - return new SearchConstructibleBsonElement("in", new Document("path", notNull("path", path).toValue()) - .append("value", BsonNull.VALUE)); - } - /** * Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of * * the field equals any value in the specified array. diff --git a/driver-core/src/test/functional/com/mongodb/client/model/search/AggregatesSearchIntegrationTest.java b/driver-core/src/test/functional/com/mongodb/client/model/search/AggregatesSearchIntegrationTest.java index 93cd1e80f49..e24fd9fa660 100644 --- a/driver-core/src/test/functional/com/mongodb/client/model/search/AggregatesSearchIntegrationTest.java +++ b/driver-core/src/test/functional/com/mongodb/client/model/search/AggregatesSearchIntegrationTest.java @@ -81,7 +81,6 @@ import static com.mongodb.client.model.search.SearchOperator.dateRange; import static com.mongodb.client.model.search.SearchOperator.exists; import static com.mongodb.client.model.search.SearchOperator.in; -import static com.mongodb.client.model.search.SearchOperator.inNull; import static com.mongodb.client.model.search.SearchOperator.near; import static com.mongodb.client.model.search.SearchOperator.numberRange; import static com.mongodb.client.model.search.SearchOperator.text; @@ -612,8 +611,7 @@ private static Stream searchAndSearchMetaArgs() { near(0, 1.5, fieldPath("fieldName7"), fieldPath("fieldName8")), near(Instant.ofEpochMilli(1), Duration.ofMillis(3), fieldPath("fieldName9")), in(fieldPath("fieldName10"), true), - in(fieldPath("fieldName11"), "term4", "term5"), - inNull(fieldPath("fieldName12")) + in(fieldPath("fieldName11"), "term4", "term5") )) .minimumShouldMatch(1) .mustNot(singleton( diff --git a/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java b/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java index 504a881d954..9852f7826a0 100644 --- a/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java +++ b/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java @@ -29,7 +29,6 @@ import org.bson.BsonDouble; import org.bson.BsonInt32; import org.bson.BsonInt64; -import org.bson.BsonNull; import org.bson.BsonObjectId; import org.bson.BsonString; import org.bson.Document; @@ -673,14 +672,6 @@ void in() { ), SearchOperator.in(fieldPath("fieldName1"), "value") .toBsonDocument() - ), - () -> assertEquals( - new BsonDocument("in", - new BsonDocument("path", fieldPath("fieldName1").toBsonValue()) - .append("value", BsonNull.VALUE) - ), - SearchOperator.inNull(fieldPath("fieldName1")) - .toBsonDocument() ) ); } diff --git a/driver-scala/src/main/scala/org/mongodb/scala/model/search/SearchOperator.scala b/driver-scala/src/main/scala/org/mongodb/scala/model/search/SearchOperator.scala index ee6521ea193..e42462dda7d 100644 --- a/driver-scala/src/main/scala/org/mongodb/scala/model/search/SearchOperator.scala +++ b/driver-scala/src/main/scala/org/mongodb/scala/model/search/SearchOperator.scala @@ -314,16 +314,6 @@ object SearchOperator { def in(path: FieldSearchPath, value: String, values: String*): InSearchOperator = JSearchOperator.in(path, value, values: _*) - /** - * Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of - * the field equals null. - * - * @param path The indexed field to be searched. - * @return The requested `SearchOperator`. - * @see [[https://www.mongodb.com/docs/atlas/atlas-search/in/ in operator]] - */ - def inNull(path: FieldSearchPath): InSearchOperator = JSearchOperator.inNull(path) - /** * Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of * the field equals any value in the specified array. From 318e55a49f25ff80b8f56919883ff76b23beb0ed Mon Sep 17 00:00:00 2001 From: Joy Kim Date: Wed, 15 Jan 2025 14:18:53 -0500 Subject: [PATCH 03/12] fix doc --- .../main/com/mongodb/client/model/search/SearchOperator.java | 4 ++-- .../scala/org/mongodb/scala/model/search/SearchOperator.scala | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java b/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java index 4267afbe2af..02af997366f 100644 --- a/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java +++ b/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java @@ -393,10 +393,10 @@ static InSearchOperator in(final FieldSearchPath path, final String value, final /** * Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of - * * the field equals any value in the specified array. + * the field equals any value in the specified array. * * @param path The indexed field to be searched. - * @param values The non-empty values to search for. + * @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. * @return The requested {@link SearchOperator}. * @mongodb.atlas.manual atlas-search/in/ in operator */ diff --git a/driver-scala/src/main/scala/org/mongodb/scala/model/search/SearchOperator.scala b/driver-scala/src/main/scala/org/mongodb/scala/model/search/SearchOperator.scala index e42462dda7d..4b5621a34b0 100644 --- a/driver-scala/src/main/scala/org/mongodb/scala/model/search/SearchOperator.scala +++ b/driver-scala/src/main/scala/org/mongodb/scala/model/search/SearchOperator.scala @@ -319,7 +319,7 @@ object SearchOperator { * the field equals any value in the specified array. * * @param path The indexed field to be searched. - * @param values The non-empty values to search for. + * @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. * @return The requested `SearchOperator`. * @see [[https://www.mongodb.com/docs/atlas/atlas-search/in/ in operator]] */ From 503a55658ad209c3c2263de09acf6d08c2d8281e Mon Sep 17 00:00:00 2001 From: Joy Kim Date: Wed, 15 Jan 2025 14:37:59 -0500 Subject: [PATCH 04/12] fix --- .../com/mongodb/client/model/search/SearchOperatorTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java b/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java index 3671acad9b1..715a2e2f5fc 100644 --- a/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java +++ b/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java @@ -676,6 +676,7 @@ void in() { ); } + @Test void phrase() { assertAll( () -> assertThrows(IllegalArgumentException.class, () -> @@ -742,6 +743,7 @@ void phrase() { ) ); } + private static SearchOperator docExamplePredefined() { return SearchOperator.exists( fieldPath("fieldName")); From b40417527fd9277e23da7824128877fb45574c7e Mon Sep 17 00:00:00 2001 From: Joy Kim Date: Wed, 15 Jan 2025 14:41:58 -0500 Subject: [PATCH 05/12] fix --- .../client/model/search/SearchOperatorTest.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java b/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java index 715a2e2f5fc..b253edc6619 100644 --- a/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java +++ b/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java @@ -693,8 +693,8 @@ void phrase() { .append("query", new BsonString("term")) ), SearchOperator.phrase( - fieldPath("fieldName"), - "term") + fieldPath("fieldName"), + "term") .toBsonDocument() ), () -> assertEquals( @@ -722,8 +722,8 @@ void phrase() { .append("synonyms", new BsonString("synonymMappingName")) ), SearchOperator.phrase( - singleton(fieldPath("fieldName")), - singleton("term")) + singleton(fieldPath("fieldName")), + singleton("term")) .synonyms("synonymMappingName") .toBsonDocument() ), @@ -735,8 +735,8 @@ void phrase() { .append("slop", new BsonInt32(5)) ), SearchOperator.phrase( - singleton(fieldPath("fieldName")), - singleton("term")) + singleton(fieldPath("fieldName")), + singleton("term")) .synonyms("synonymMappingName") .slop(5) .toBsonDocument() From 2e31990c273783368b7fef6f49d007863f181da3 Mon Sep 17 00:00:00 2001 From: Joy Kim Date: Wed, 15 Jan 2025 15:38:56 -0500 Subject: [PATCH 06/12] fix checkstyle --- driver-kotlin-extensions/build.gradle.kts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/driver-kotlin-extensions/build.gradle.kts b/driver-kotlin-extensions/build.gradle.kts index e0192fa5abc..76f36ca33b2 100644 --- a/driver-kotlin-extensions/build.gradle.kts +++ b/driver-kotlin-extensions/build.gradle.kts @@ -165,4 +165,6 @@ tasks.javadocJar.configure { // =========================== tasks.sourcesJar { from(project.sourceSets.main.map { it.kotlin }) } -afterEvaluate { tasks.jar { manifest { attributes["Automatic-Module-Name"] = "org.mongodb.driver.kotlin.extensions" } } } +afterEvaluate { + tasks.jar { manifest { attributes["Automatic-Module-Name"] = "org.mongodb.driver.kotlin.extensions" } } +} From 42da7affff1c1c432a5e36026417ee9a53847098 Mon Sep 17 00:00:00 2001 From: Valentin Kovalenko Date: Thu, 16 Jan 2025 15:32:36 -0700 Subject: [PATCH 07/12] Stop manually encoding UUIDs --- .../mongodb/client/model/search/SearchOperator.java | 12 +----------- .../client/model/search/SearchOperatorTest.java | 7 ++++++- 2 files changed, 7 insertions(+), 12 deletions(-) diff --git a/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java b/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java index 6d8c612151f..882f6e66ee9 100644 --- a/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java +++ b/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java @@ -21,11 +21,8 @@ import com.mongodb.client.model.Aggregates; import com.mongodb.client.model.geojson.Point; -import java.util.ArrayList; -import java.util.List; import java.util.UUID; -import org.bson.BsonBinary; import org.bson.BsonType; import org.bson.Document; import org.bson.conversions.Bson; @@ -367,14 +364,7 @@ static InSearchOperator in(final FieldSearchPath path, final Instant value, fina * @mongodb.atlas.manual atlas-search/in/ in operator */ static InSearchOperator in(final FieldSearchPath path, final UUID value, final UUID... values) { - List bsonValues = new ArrayList<>(); - bsonValues.add(new BsonBinary(value)); - if (values != null) { - for (UUID uuid : values) { - bsonValues.add(new BsonBinary(uuid)); - } - } - return in(notNull("path", path), notNull("value", bsonValues)); + return in(notNull("path", path), concat(notNull("value", value), values)); } /** diff --git a/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java b/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java index b253edc6619..33f0e8e7127 100644 --- a/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java +++ b/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java @@ -32,6 +32,9 @@ import org.bson.BsonObjectId; import org.bson.BsonString; import org.bson.Document; +import org.bson.UuidRepresentation; +import org.bson.codecs.configuration.CodecRegistries; +import org.bson.conversions.Bson; import org.bson.types.ObjectId; import org.junit.jupiter.api.Test; @@ -663,7 +666,9 @@ void in() { .append("value", new BsonBinary(uuid)) ), SearchOperator.in(fieldPath("fieldName1"), uuid) - .toBsonDocument() + .toBsonDocument( + Document.class, + CodecRegistries.withUuidRepresentation(Bson.DEFAULT_CODEC_REGISTRY, UuidRepresentation.STANDARD)) ), () -> assertEquals( new BsonDocument("in", From 65840ebcafc785839823775eda11a8d11dd2ea1c Mon Sep 17 00:00:00 2001 From: Maxim Katcharov Date: Fri, 24 Jan 2025 13:46:45 -0700 Subject: [PATCH 08/12] PR fixes --- .../client/model/search/SearchOperator.java | 68 ++++++++++--------- .../scala/model/search/SearchOperator.scala | 28 ++++---- 2 files changed, 51 insertions(+), 45 deletions(-) diff --git a/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java b/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java index 882f6e66ee9..fb40a3bfe04 100644 --- a/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java +++ b/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java @@ -18,26 +18,29 @@ import com.mongodb.annotations.Beta; import com.mongodb.annotations.Reason; import com.mongodb.annotations.Sealed; +import com.mongodb.assertions.Assertions; import com.mongodb.client.model.Aggregates; import com.mongodb.client.model.geojson.Point; - -import java.util.UUID; - +import org.bson.BsonArray; +import org.bson.BsonBoolean; import org.bson.BsonType; +import org.bson.BsonValue; import org.bson.Document; import org.bson.conversions.Bson; +import org.bson.types.ObjectId; import java.time.Duration; import java.time.Instant; +import java.util.ArrayList; import java.util.Iterator; - -import org.bson.types.ObjectId; +import java.util.List; +import java.util.UUID; import static com.mongodb.assertions.Assertions.isTrueArgument; +import static com.mongodb.assertions.Assertions.notNull; import static com.mongodb.internal.Iterables.concat; import static com.mongodb.internal.client.model.Util.combineToBsonValue; import static java.util.Collections.singleton; -import static com.mongodb.assertions.Assertions.notNull; /** * The core part of the {@link Aggregates#search(SearchOperator, SearchOptions) $search} pipeline stage of an aggregation pipeline. @@ -298,8 +301,8 @@ static GeoNearSearchOperator near(final Point origin, final Number pivot, final } /** - * Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of - * the field equals any value in the specified array. + * Returns a {@link SearchOperator} that searches for documents where the + * value or array of values at a given path contains any of the specified values * * @param path The indexed field to be searched. * @param value The boolean value to search for. @@ -307,13 +310,20 @@ static GeoNearSearchOperator near(final Point origin, final Number pivot, final * @return The requested {@link SearchOperator}. * @mongodb.atlas.manual atlas-search/in/ in operator */ - static InSearchOperator in(final FieldSearchPath path, final Boolean value, final Boolean... values) { - return in(notNull("path", path), concat(notNull("value", value), values)); + static InSearchOperator in(final FieldSearchPath path, final boolean value, final boolean... values) { + Assertions.notNull("values", values); + List list = new ArrayList<>(); + list.add(new BsonBoolean(value)); + for (boolean n : values) { + list.add(new BsonBoolean(n)); + } + BsonArray bsonArray = new BsonArray(list); + return in(notNull("path", path), bsonArray); } /** - * Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of - * the field equals any value in the specified array. + * Returns a {@link SearchOperator} that searches for documents where the + * value or array of values at a given path contains any of the specified values * * @param path The indexed field to be searched. * @param value The objectId value to search for. @@ -326,8 +336,8 @@ static InSearchOperator in(final FieldSearchPath path, final ObjectId value, fin } /** - * Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of - * the field equals any value in the specified array. + * Returns a {@link SearchOperator} that searches for documents where the + * value or array of values at a given path contains any of the specified values * * @param path The indexed field to be searched. * @param value The number value to search for. @@ -340,8 +350,8 @@ static InSearchOperator in(final FieldSearchPath path, final Number value, final } /** - * Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of - * the field equals any value in the specified array. + * Returns a {@link SearchOperator} that searches for documents where the + * value or array of values at a given path contains any of the specified values * * @param path The indexed field to be searched. * @param value The instant date value to search for. @@ -354,8 +364,8 @@ static InSearchOperator in(final FieldSearchPath path, final Instant value, fina } /** - * Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of - * the field equals any value in the specified array. + * Returns a {@link SearchOperator} that searches for documents where the + * value or array of values at a given path contains any of the specified values * * @param path The indexed field to be searched. * @param value The uuid value to search for. @@ -368,8 +378,8 @@ static InSearchOperator in(final FieldSearchPath path, final UUID value, final U } /** - * Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of - * the field equals any value in the specified array. + * Returns a {@link SearchOperator} that searches for documents where the + * value or array of values at a given path contains any of the specified values * * @param path The indexed field to be searched. * @param value The string value to search for. @@ -382,8 +392,8 @@ static InSearchOperator in(final FieldSearchPath path, final String value, final } /** - * Returns a {@link SearchOperator} that searches for an array of values at the given path and returns documents where the value of - * the field equals any value in the specified array. + * Returns a {@link SearchOperator} that searches for documents where the + * value or array of values at a given path contains any of the specified values * * @param path The indexed field to be searched. * @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. @@ -391,18 +401,14 @@ static InSearchOperator in(final FieldSearchPath path, final String value, final * @mongodb.atlas.manual atlas-search/in/ in operator */ static InSearchOperator in(final FieldSearchPath path, final Iterable values) { + notNull("path", path); Iterator valueIterator = (Iterator) notNull("values", values).iterator(); isTrueArgument("values must not be empty", valueIterator.hasNext()); T firstValue = valueIterator.next(); - - Iterator typeIterator = (Iterator) notNull("values", values).iterator(); - while (typeIterator.hasNext()) { - Object element = typeIterator.next(); - isTrueArgument("values must be of same type", firstValue.getClass().isInstance(element)); - } - - return new SearchConstructibleBsonElement("in", new Document("path", notNull("path", path).toValue()) - .append("value", valueIterator.hasNext() ? values : firstValue)); + boolean hasMore = valueIterator.hasNext(); + return new SearchConstructibleBsonElement("in", new Document() + .append("path", path.toValue()) + .append("value", hasMore ? values : firstValue)); } /** diff --git a/driver-scala/src/main/scala/org/mongodb/scala/model/search/SearchOperator.scala b/driver-scala/src/main/scala/org/mongodb/scala/model/search/SearchOperator.scala index 661234b0dde..5af14b02ca4 100644 --- a/driver-scala/src/main/scala/org/mongodb/scala/model/search/SearchOperator.scala +++ b/driver-scala/src/main/scala/org/mongodb/scala/model/search/SearchOperator.scala @@ -237,8 +237,8 @@ object SearchOperator { JSearchOperator.near(origin, pivot, paths.asJava) /** - * Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of - * the field equals any value in the specified array. + * Returns a `SearchOperator` that searches for documents where the value + * or array of values at a given path contains any of the specified values * * @param path The indexed field to be searched. * @param value The boolean value to search for. @@ -250,8 +250,8 @@ object SearchOperator { JSearchOperator.in(path, value, values: _*) /** - * Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of - * the field equals any value in the specified array. + * Returns a `SearchOperator` that searches for documents where the value + * or array of values at a given path contains any of the specified values * * @param path The indexed field to be searched. * @param value The objectId value to search for. @@ -263,8 +263,8 @@ object SearchOperator { JSearchOperator.in(path, value, values: _*) /** - * Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of - * the field equals any value in the specified array. + * Returns a `SearchOperator` that searches for documents where the value + * or array of values at a given path contains any of the specified values * * @param path The indexed field to be searched. * @param value The number value to search for. @@ -276,8 +276,8 @@ object SearchOperator { JSearchOperator.in(path, value, values: _*) /** - * Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of - * the field equals any value in the specified array. + * Returns a `SearchOperator` that searches for documents where the value + * or array of values at a given path contains any of the specified values * * @param path The indexed field to be searched. * @param value The instant date value to search for. @@ -289,8 +289,8 @@ object SearchOperator { JSearchOperator.in(path, value, values: _*) /** - * Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of - * the field equals any value in the specified array. + * Returns a `SearchOperator` that searches for documents where the value + * or array of values at a given path contains any of the specified values * * @param path The indexed field to be searched. * @param value The uuid value to search for. @@ -302,8 +302,8 @@ object SearchOperator { JSearchOperator.in(path, value, values: _*) /** - * Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of - * the field equals any value in the specified array. + * Returns a `SearchOperator` that searches for documents where the value + * or array of values at a given path contains any of the specified values * * @param path The indexed field to be searched. * @param value The string value to search for. @@ -315,8 +315,8 @@ object SearchOperator { JSearchOperator.in(path, value, values: _*) /** - * Returns a `SearchOperator` that searches for an array of values at the given path and returns documents where the value of - * the field equals any value in the specified array. + * Returns a `SearchOperator` that searches for documents where the value + * or array of values at a given path contains any of the specified values * * @param path The indexed field to be searched. * @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. From aa39619e052977e823afc567f65630d7a2bf617e Mon Sep 17 00:00:00 2001 From: Maxim Katcharov Date: Wed, 29 Jan 2025 10:47:50 -0700 Subject: [PATCH 09/12] Update driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java Co-authored-by: Valentin Kovalenko --- .../main/com/mongodb/client/model/search/SearchOperator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java b/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java index fb40a3bfe04..e24519d4130 100644 --- a/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java +++ b/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java @@ -402,7 +402,7 @@ static InSearchOperator in(final FieldSearchPath path, final String value, final */ static InSearchOperator in(final FieldSearchPath path, final Iterable values) { notNull("path", path); - Iterator valueIterator = (Iterator) notNull("values", values).iterator(); + Iterator valueIterator = notNull("values", values).iterator(); isTrueArgument("values must not be empty", valueIterator.hasNext()); T firstValue = valueIterator.next(); boolean hasMore = valueIterator.hasNext(); From 44c43e109ba7cf37a9cf2c0c1603b526a25f2a7f Mon Sep 17 00:00:00 2001 From: Maxim Katcharov Date: Wed, 29 Jan 2025 11:06:44 -0700 Subject: [PATCH 10/12] PR fixes --- .../main/org/bson/codecs/BsonArrayCodec.java | 12 ++------ .../mongodb/client/model/mql/MqlValues.java | 30 +++++++++---------- .../client/model/search/SearchOperator.java | 15 ++++------ .../model/search/SearchOperatorTest.java | 8 +---- 4 files changed, 24 insertions(+), 41 deletions(-) diff --git a/bson/src/main/org/bson/codecs/BsonArrayCodec.java b/bson/src/main/org/bson/codecs/BsonArrayCodec.java index 2efe3147d8a..9b4bef5e4c5 100644 --- a/bson/src/main/org/bson/codecs/BsonArrayCodec.java +++ b/bson/src/main/org/bson/codecs/BsonArrayCodec.java @@ -23,9 +23,6 @@ import org.bson.BsonWriter; import org.bson.codecs.configuration.CodecRegistry; -import java.util.ArrayList; -import java.util.List; - import static org.bson.assertions.Assertions.notNull; import static org.bson.codecs.configuration.CodecRegistries.fromProviders; @@ -60,16 +57,13 @@ public BsonArrayCodec(final CodecRegistry codecRegistry) { @Override public BsonArray decode(final BsonReader reader, final DecoderContext decoderContext) { + BsonArray bsonArray = new BsonArray(); reader.readStartArray(); - - List list = new ArrayList<>(); while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) { - list.add(readValue(reader, decoderContext)); + bsonArray.add(readValue(reader, decoderContext)); } - reader.readEndArray(); - - return new BsonArray(list); + return bsonArray; } @Override diff --git a/driver-core/src/main/com/mongodb/client/model/mql/MqlValues.java b/driver-core/src/main/com/mongodb/client/model/mql/MqlValues.java index a2d58fbc02b..e3e2bbd56a2 100644 --- a/driver-core/src/main/com/mongodb/client/model/mql/MqlValues.java +++ b/driver-core/src/main/com/mongodb/client/model/mql/MqlValues.java @@ -74,11 +74,11 @@ public static MqlBoolean of(final boolean of) { */ public static MqlArray ofBooleanArray(final boolean... array) { Assertions.notNull("array", array); - List list = new ArrayList<>(); + BsonArray bsonArray = new BsonArray(); for (boolean b : array) { - list.add(new BsonBoolean(b)); + bsonArray.add(new BsonBoolean(b)); } - return new MqlExpression<>((cr) -> new AstPlaceholder(new BsonArray(list))); + return new MqlExpression<>((cr) -> new AstPlaceholder(bsonArray)); } /** @@ -102,11 +102,11 @@ public static MqlInteger of(final int of) { */ public static MqlArray ofIntegerArray(final int... array) { Assertions.notNull("array", array); - List list = new ArrayList<>(); + BsonArray bsonArray = new BsonArray(); for (int i : array) { - list.add(new BsonInt32(i)); + bsonArray.add(new BsonInt32(i)); } - return new MqlExpression<>((cr) -> new AstPlaceholder(new BsonArray(list))); + return new MqlExpression<>((cr) -> new AstPlaceholder(bsonArray)); } /** @@ -130,11 +130,11 @@ public static MqlInteger of(final long of) { */ public static MqlArray ofIntegerArray(final long... array) { Assertions.notNull("array", array); - List list = new ArrayList<>(); + BsonArray bsonArray = new BsonArray(); for (long i : array) { - list.add(new BsonInt64(i)); + bsonArray.add(new BsonInt64(i)); } - return new MqlExpression<>((cr) -> new AstPlaceholder(new BsonArray(list))); + return new MqlExpression<>((cr) -> new AstPlaceholder(bsonArray)); } /** @@ -158,11 +158,11 @@ public static MqlNumber of(final double of) { */ public static MqlArray ofNumberArray(final double... array) { Assertions.notNull("array", array); - List list = new ArrayList<>(); + BsonArray bsonArray = new BsonArray(); for (double n : array) { - list.add(new BsonDouble(n)); + bsonArray.add(new BsonDouble(n)); } - return new MqlExpression<>((cr) -> new AstPlaceholder(new BsonArray(list))); + return new MqlExpression<>((cr) -> new AstPlaceholder(bsonArray)); } /** @@ -310,12 +310,12 @@ public static MqlDocument current() { public static MqlArray ofArray(final T... array) { Assertions.notNull("array", array); return new MqlExpression<>((cr) -> { - List list = new ArrayList<>(); + BsonArray bsonArray = new BsonArray(); for (T v : array) { Assertions.notNull("elements of array", v); - list.add(((MqlExpression) v).toBsonValue(cr)); + bsonArray.add(((MqlExpression) v).toBsonValue(cr)); } - return new AstPlaceholder(new BsonArray(list)); + return new AstPlaceholder(bsonArray); }); } diff --git a/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java b/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java index e24519d4130..f0beca8e02b 100644 --- a/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java +++ b/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java @@ -18,22 +18,18 @@ import com.mongodb.annotations.Beta; import com.mongodb.annotations.Reason; import com.mongodb.annotations.Sealed; -import com.mongodb.assertions.Assertions; import com.mongodb.client.model.Aggregates; import com.mongodb.client.model.geojson.Point; import org.bson.BsonArray; import org.bson.BsonBoolean; import org.bson.BsonType; -import org.bson.BsonValue; import org.bson.Document; import org.bson.conversions.Bson; import org.bson.types.ObjectId; import java.time.Duration; import java.time.Instant; -import java.util.ArrayList; import java.util.Iterator; -import java.util.List; import java.util.UUID; import static com.mongodb.assertions.Assertions.isTrueArgument; @@ -311,13 +307,12 @@ static GeoNearSearchOperator near(final Point origin, final Number pivot, final * @mongodb.atlas.manual atlas-search/in/ in operator */ static InSearchOperator in(final FieldSearchPath path, final boolean value, final boolean... values) { - Assertions.notNull("values", values); - List list = new ArrayList<>(); - list.add(new BsonBoolean(value)); - for (boolean n : values) { - list.add(new BsonBoolean(n)); + notNull("values", values); + BsonArray bsonArray = new BsonArray(); + bsonArray.add(new BsonBoolean(value)); + for (boolean v : values) { + bsonArray.add(new BsonBoolean(v)); } - BsonArray bsonArray = new BsonArray(list); return in(notNull("path", path), bsonArray); } diff --git a/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java b/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java index 33f0e8e7127..ded32874804 100644 --- a/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java +++ b/driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java @@ -18,9 +18,6 @@ import com.mongodb.MongoClientSettings; import com.mongodb.client.model.geojson.Point; import com.mongodb.client.model.geojson.Position; - -import java.util.UUID; - import org.bson.BsonArray; import org.bson.BsonBinary; import org.bson.BsonBoolean; @@ -40,6 +37,7 @@ import java.time.Duration; import java.time.Instant; +import java.util.UUID; import static com.mongodb.client.model.search.FuzzySearchOptions.fuzzySearchOptions; import static com.mongodb.client.model.search.SearchPath.fieldPath; @@ -600,10 +598,6 @@ void in() { // paths must not be empty SearchOperator.in(null, true) ), - () -> assertThrows(IllegalArgumentException.class, () -> - // values should be of one supported BSON types and can't be a mix of different types - SearchOperator.in(fieldPath("fieldName1"), asList(true, "value")) - ), () -> assertEquals( new BsonDocument("in", new BsonDocument("path", fieldPath("fieldName1").toBsonValue()) From d0dfa2b2de7c7bbe8eb055425405449c96c13707 Mon Sep 17 00:00:00 2001 From: Maxim Katcharov Date: Wed, 29 Jan 2025 13:52:41 -0700 Subject: [PATCH 11/12] Post-merge fixes --- .../com/mongodb/client/model/search/InSearchOperator.java | 8 +++----- .../com/mongodb/client/model/search/SearchOperator.java | 1 + 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/driver-core/src/main/com/mongodb/client/model/search/InSearchOperator.java b/driver-core/src/main/com/mongodb/client/model/search/InSearchOperator.java index 4540db60f1f..4719d1b0bc6 100644 --- a/driver-core/src/main/com/mongodb/client/model/search/InSearchOperator.java +++ b/driver-core/src/main/com/mongodb/client/model/search/InSearchOperator.java @@ -18,15 +18,13 @@ import com.mongodb.annotations.Beta; import com.mongodb.annotations.Reason; import com.mongodb.annotations.Sealed; - -import java.util.UUID; +import org.bson.types.ObjectId; import java.time.Instant; - -import org.bson.types.ObjectId; +import java.util.UUID; /** - * @see SearchOperator#in(FieldSearchPath, Boolean, Boolean...) + * @see SearchOperator#in(FieldSearchPath, boolean, boolean...) * @see SearchOperator#in(FieldSearchPath, ObjectId, ObjectId...) * @see SearchOperator#in(FieldSearchPath, Number, Number...) * @see SearchOperator#in(FieldSearchPath, Instant, Instant...) diff --git a/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java b/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java index 69391291db1..e9aae9fa7cb 100644 --- a/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java +++ b/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java @@ -395,6 +395,7 @@ static InSearchOperator in(final FieldSearchPath path, final String value, final * * @param path The indexed field to be searched. * @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. + * @param the type. * @return The requested {@link SearchOperator}. * @mongodb.atlas.manual atlas-search/in/ in operator */ From ec1e7133b639d014d76d226dd113691a72eae507 Mon Sep 17 00:00:00 2001 From: Maxim Katcharov Date: Mon, 3 Feb 2025 14:08:43 -0700 Subject: [PATCH 12/12] Update driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java Co-authored-by: Valentin Kovalenko --- .../main/com/mongodb/client/model/search/SearchOperator.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java b/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java index e9aae9fa7cb..ef5c1239313 100644 --- a/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java +++ b/driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java @@ -395,7 +395,7 @@ static InSearchOperator in(final FieldSearchPath path, final String value, final * * @param path The indexed field to be searched. * @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. - * @param the type. + * @param the type of elements in {@code values}. * @return The requested {@link SearchOperator}. * @mongodb.atlas.manual atlas-search/in/ in operator */