Skip to content
Merged
54 changes: 54 additions & 0 deletions source/examples/atlas-examples/AtlasSearchHelpers.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import com.mongodb.client.model.Aggregates
import com.mongodb.client.model.Projections
import com.mongodb.client.model.search.SearchOperator
import com.mongodb.client.model.search.SearchPath.fieldPath
import com.mongodb.kotlin.client.coroutine.MongoClient
import kotlinx.coroutines.runBlocking
import org.bson.Document
import org.bson.conversions.Bson
import java.io.IO.println
import kotlin.collections.List

const val URI = "<connection-string>"

// Create data class to represent a MongoDB document
data class Movie(val title: String, val year: Int, val cast: List<String>)

fun main() {

// Replace the placeholder with your MongoDB deployment's connection string
val uri = URI

val mongoClient = MongoClient.create(uri)
val database = mongoClient.getDatabase("sample_mflix")
// Get a collection of documents of type Movie
val collection = database.getCollection<Movie>("movies")

// start atlasHelperMethods
runBlocking {
val searchStage: Bson = Aggregates.search(
SearchOperator.compound()
.filter(
listOf(
SearchOperator.text(fieldPath("genres"), "Drama"),
SearchOperator.phrase(fieldPath("cast"), "sylvester stallone"),
SearchOperator.numberRange(fieldPath("year")).gtLt(1980, 1989),
SearchOperator.wildcard(fieldPath("title"), "Rocky *")
)
)
)

val projection = Projections.fields(
Projections.include("title", "year", "genres", "cast")
)

val aggregatePipelineStages: List<Bson> = listOf(searchStage, Aggregates.project(projection))
val results = collection.aggregate<Document>(aggregatePipelineStages)

results.collect { println(it) }
}
// end atlasHelperMethods

mongoClient.close()
}

39 changes: 39 additions & 0 deletions source/fundamentals/aggregation.txt
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,49 @@ first element in the ``categories`` field.
Results(name=456 Cookies Shop, firstCategory=Bakery)
Results(name=XYZ Steak Buffet, firstCategory=Steak)

.. _kotlin-cr-atlas-search-stage:

Pipelines Stages for Atlas Search
---------------------------------

:atlas:`Atlas Search </atlas-search>` queries take the form of an aggregation pipeline stage. Atlas
Search provides ``$search`` and ``$searchMeta`` stages, both of which must be the first
stage in any query pipeline. For more information about Atlas pipeline stages,
see the :atlas:`Choose the Aggregation Pipeline Stage
</atlas-search/query-syntax/>` page in the Atlas
manual.

.. sharedinclude:: dbx/jvm/atlas-search-operator-helpers.rst

.. replacement:: atlas-query-operators-example

.. io-code-block::

.. input:: /examples/atlas-examples/AtlasSearchHelpers.kt
:language: kotlin
:start-after: // start atlasHelperMethods
:end-before: // end atlasHelperMethods
:dedent:

.. output::
:language: console
:visible: false

Document{{_id=573a1397f29313caabce86db, genres=[Drama, Sport], cast=[Sylvester Stallone, Talia Shire, Burt Young, Carl Weathers], title=Rocky III, year=1982}}
Document{{_id=573a1398f29313caabce9af0, genres=[Drama, Sport], cast=[Sylvester Stallone, Talia Shire, Burt Young, Carl Weathers], title=Rocky IV, year=1985}}

.. replacement:: searchoperator-interface-api-docs

the `SearchOperator Interface API documentation <{+core-api+}/client/model/search/SearchOperator.html>`__

API Documentation
-----------------

For more information about the methods and classes mentioned in this section,
see the following API Documentation:

- `Accumulators <{+core-api+}/client/model/Accumulators.html>`__
- `$group <{+core-api+}/client/model/Aggregates.html#group(TExpression,java.util.List)>`__
- `$project <{+core-api+}/client/model/Aggregates.html#project(org.bson.conversions.Bson)>`__
- `Projections <{+core-api+}/client/model/Projections.html>`__
- `SearchOperator Interface <{+core-api+}/com/mongodb/client/model/search/SearchOperator.html>`__
5 changes: 5 additions & 0 deletions source/whats-new.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ and features:
the `SearchOperator <{+core-api+}/client/model/search/SearchOperator.html>`__
interface API documentation

.. replacement:: atlas-query-operators

the :ref:`Pipelines Stages for Atlas Search
<kotlin-cr-atlas-search-stage>` section of the Aggregation page

.. _kotlin-coroutine-version-5.3:

What's New in 5.3
Expand Down
Loading