Skip to content

Commit 7966b0c

Browse files
committed
eg
1 parent 314b9cb commit 7966b0c

File tree

2 files changed

+69
-15
lines changed

2 files changed

+69
-15
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import com.mongodb.client.model.Aggregates
2+
import com.mongodb.client.model.Projections
3+
import com.mongodb.client.model.search.SearchOperator
4+
import com.mongodb.client.model.search.SearchPath.fieldPath
5+
import com.mongodb.kotlin.client.coroutine.MongoClient
6+
import kotlinx.coroutines.runBlocking
7+
import org.bson.Document
8+
import org.bson.conversions.Bson
9+
import java.io.IO.println
10+
import kotlin.collections.List
11+
12+
const val URI = "<connection-string>"
13+
14+
// Create data class to represent a MongoDB document
15+
data class Movie(val title: String, val year: Int, val cast: List<String>)
16+
17+
fun main() {
18+
19+
// Replace the placeholder with your MongoDB deployment's connection string
20+
val uri = URI
21+
22+
val mongoClient = MongoClient.create(uri)
23+
val database = mongoClient.getDatabase("sample_mflix")
24+
// Get a collection of documents of type Movie
25+
val collection = database.getCollection<Movie>("movies")
26+
27+
// start atlasHelperMethods
28+
runBlocking {
29+
val searchStage: Bson = Aggregates.search(
30+
SearchOperator.compound()
31+
.filter(
32+
listOf(
33+
SearchOperator.text(fieldPath("genres"), "Drama"),
34+
SearchOperator.phrase(fieldPath("cast"), "sylvester stallone"),
35+
SearchOperator.numberRange(fieldPath("year")).gtLt(1980, 1989),
36+
SearchOperator.wildcard(fieldPath("title"), "Rocky *")
37+
)
38+
)
39+
)
40+
41+
val projection = Projections.fields(
42+
Projections.include("title", "year", "genres", "cast")
43+
)
44+
45+
val aggregatePipelineStages: List<Bson> = listOf(searchStage, Aggregates.project(projection))
46+
val results = collection.aggregate<Document>(aggregatePipelineStages)
47+
48+
results.collect { println(it) }
49+
}
50+
// end atlasHelperMethods
51+
52+
mongoClient.close()
53+
}
54+

source/fundamentals/aggregation.txt

+15-15
Original file line numberDiff line numberDiff line change
@@ -231,24 +231,24 @@ manual.
231231

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

234-
.. code-block:: kotlin
235-
236-
val searchStage: Bson = Aggregates.search(
237-
SearchOperator.compound()
238-
.filter(
239-
listOf(
240-
SearchOperator.text(fieldPath("genres"), "Drama"),
241-
SearchOperator.phrase(fieldPath("cast"), "sylvester stallone"),
242-
SearchOperator.numberRange(fieldPath("year")).gtLt(1980, 1989),
243-
SearchOperator.wildcard(fieldPath("title"), "Rocky *")
244-
)
245-
)
246-
)
234+
.. io-code-block::
235+
236+
.. input:: /examples/atlas-examples/AtlasSearchHelpers.kt
237+
:language: kotlin
238+
:start-after: // start atlasHelperMethods
239+
:end-before: // end atlasHelperMethods
240+
:dedent:
241+
242+
.. output::
243+
:language: console
244+
:visible: false
245+
246+
Document{{_id=573a1397f29313caabce86db, genres=[Drama, Sport], cast=[Sylvester Stallone, Talia Shire, Burt Young, Carl Weathers], title=Rocky III, year=1982}}
247+
Document{{_id=573a1398f29313caabce9af0, genres=[Drama, Sport], cast=[Sylvester Stallone, Talia Shire, Burt Young, Carl Weathers], title=Rocky IV, year=1985}}
247248

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

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

253253
API Documentation
254254
-----------------

0 commit comments

Comments
 (0)