diff --git a/source/aggregation.txt b/source/aggregation.txt index 79d11014..34592cf2 100644 --- a/source/aggregation.txt +++ b/source/aggregation.txt @@ -9,7 +9,7 @@ Transform Your Data with Aggregation :values: reference .. meta:: - :keywords: code example, transform, computed, pipeline + :keywords: code example, transform, computed, pipeline, Atlas Search :description: Learn how to use the Kotlin Sync driver to perform aggregation operations. .. contents:: On this page @@ -80,6 +80,7 @@ The following limitations apply when using aggregation operations: ` stage has a strict memory limit of 100 megabytes and ignores the ``allowDiskUse`` option. + Aggregation Example ------------------- @@ -188,6 +189,50 @@ and adds the ``$explain`` stage to output the operation details: ... } +.. _kotlin-sync-atlas-search-stage: + +Atlas Search +------------ + +You can perform an :atlas:`Atlas Search ` query by creating and running +an aggregation pipeline that contains one of the following pipeline stages: + +- ``$search`` +- ``$searchMeta`` + +To learn more about Atlas Search pipeline stages, see :atlas:`Choose the +Aggregation Pipeline Stage ` in the Atlas +documentation. + +Create a Pipeline Search Stage +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +You can create the search criteria in your Atlas Search pipeline stage +by using Search operators. + +.. sharedinclude:: dbx/jvm/atlas-search-operator-helpers.rst + + .. replacement:: as-idx-link + + the :ref:`kotlin-sync-atlas-search-index` guide + + .. replacement:: atlas-query-operators-example + + .. io-code-block:: + + .. input:: /includes/aggregation/aggregation.kt + :language: kotlin + :start-after: // start-atlas-searchoperator-helpers + :end-before: // end-atlas-searchoperator-helpers + :dedent: + + .. output:: + :language: console + :visible: false + + Document{{_id=..., genres=[Comedy, Romance], title=Love at First Bite, year=1979}} + Document{{_id=..., genres=[Comedy, Drama], title=Love Affair, year=1994}} + Additional Information ---------------------- @@ -217,4 +262,7 @@ For more information about executing aggregation operations with the {+driver-sh see the following API documentation: - `aggregate() <{+driver-api+}/-mongo-collection/aggregate.html>`__ -- `AggregateIterable <{+driver-api+}/-aggregate-iterable/index.html>`__ \ No newline at end of file +- `AggregateIterable <{+driver-api+}/-aggregate-iterable/index.html>`__ +- `Aggregates <{+core-api+}/client/model/Aggregates>`__ +- `search() <{+core-api+}/client/model/Aggregates#search(com.mongodb.client.model.search.SearchOperator)>`__ +- `project() <{+core-api+}/client/model/Aggregates#project(org.bson.conversions.Bson)>`__ diff --git a/source/includes/aggregation/aggregation.kt b/source/includes/aggregation/aggregation.kt index 4b675947..a804b46e 100644 --- a/source/includes/aggregation/aggregation.kt +++ b/source/includes/aggregation/aggregation.kt @@ -4,6 +4,9 @@ import com.mongodb.client.model.Accumulators import com.mongodb.client.model.Aggregates import com.mongodb.client.model.Filters import com.mongodb.kotlin.client.MongoClient +import com.mongodb.client.model.Projections +import com.mongodb.client.model.search.SearchOperator +import com.mongodb.client.model.search.SearchPath.fieldPath import org.bson.Document // start-data-class @@ -42,5 +45,27 @@ fun main() { // start-aggregation-explain print(collection.aggregate(pipeline).explain()) // end-aggregation-explain + + // start-atlas-searchoperator-helpers + val searchStage = Aggregates.search( + SearchOperator.compound() + .filter( + listOf( + SearchOperator.`in`(fieldPath("genres"), listOf("Comedy")), + SearchOperator.phrase(fieldPath("fullplot"), "new york"), + SearchOperator.numberRange(fieldPath("year")).gtLt(1950, 2000), + SearchOperator.wildcard(fieldPath("title"), "Love *") + ) + ) + ) + + val projectStage = Aggregates.project( + Projections.include("title", "year", "genres")) + + val pipeline = listOf(searchStage, projectStage) + val results = collection.aggregate(pipeline) + + results.forEach { result -> println(result) } + // end-atlas-searchoperator-helpers } diff --git a/source/whats-new.txt b/source/whats-new.txt index 01fb6511..c237a58b 100644 --- a/source/whats-new.txt +++ b/source/whats-new.txt @@ -50,8 +50,7 @@ and features: .. replacement:: atlas-query-operators - the `SearchOperator <{+core-api+}/client/model/search/SearchOperator.html>`__ - interface API documentation + the :ref:`Atlas Search ` section of the Aggregation guide .. _kotlin-sync-version-5.3: